0
0
Blockchain / Solidityprogramming~30 mins

State variables in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding State Variables in Solidity
📖 Scenario: You are creating a simple smart contract on the Ethereum blockchain to store and retrieve a number. This number should be saved permanently on the blockchain so that anyone can see it later.
🎯 Goal: Build a Solidity smart contract that uses a state variable to store a number and a function to read that number.
📋 What You'll Learn
Create a uint state variable named storedNumber.
Write a function named setNumber that takes a uint input and updates storedNumber.
Write a function named getNumber that returns the current value of storedNumber.
💡 Why This Matters
🌍 Real World
Smart contracts use state variables to keep track of balances, ownership, and other important data that must persist on the blockchain.
💼 Career
Understanding state variables is essential for blockchain developers to create secure and functional decentralized applications.
Progress0 / 4 steps
1
Create the state variable
Create a Solidity contract named SimpleStorage. Inside it, declare a uint state variable called storedNumber.
Blockchain / Solidity
Need a hint?

State variables are declared inside the contract but outside any function.

2
Add a function to set the number
Inside the SimpleStorage contract, write a public function named setNumber that takes a uint parameter named num and assigns it to the state variable storedNumber.
Blockchain / Solidity
Need a hint?

The function should be public so anyone can call it to update the number.

3
Add a function to get the number
Inside the SimpleStorage contract, write a public view function named getNumber that returns the current value of the storedNumber state variable.
Blockchain / Solidity
Need a hint?

The view keyword means the function does not change the state.

4
Test the contract output
Write a comment showing the expected output when setNumber(42) is called and then getNumber() is called.
Blockchain / Solidity
Need a hint?

The comment should clearly show the stored number after setting it.