📖 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
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
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
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
Hint
The comment should clearly show the stored number after setting it.
Practice
(1/5)
1. What is a state variable in a blockchain smart contract?
easy
A. A variable that stores data permanently on the blockchain
B. A temporary variable used only inside functions
C. A variable that stores user input off-chain
D. A variable that resets every time a function runs
Solution
Step 1: Understand what state variables do
State variables store data permanently on the blockchain, unlike local variables which are temporary.
Step 2: Compare options with definition
Only A variable that stores data permanently on the blockchain describes permanent storage on the blockchain, which matches the definition of state variables.
Final Answer:
A variable that stores data permanently on the blockchain -> Option A
Quick Check:
State variable = permanent blockchain storage [OK]
Hint: State variables save data permanently on blockchain [OK]
Common Mistakes:
Confusing state variables with local variables
Thinking state variables reset after function ends
Assuming state variables store data off-chain
2. Which of the following is the correct way to declare a state variable in Solidity?
easy
A. let count = uint256;
B. function uint256 count;
C. uint256 public count;
D. var count = 0;
Solution
Step 1: Recall Solidity syntax for state variables
State variables are declared outside functions with a type and visibility, e.g., uint256 public count;.
Step 2: Check each option
uint256 public count; matches correct syntax. Options A, B, and C use incorrect keywords or syntax not valid for state variables.
Final Answer:
uint256 public count; -> Option C
Quick Check:
State variable declaration = type + visibility + name [OK]
Hint: State variables need type and visibility outside functions [OK]
Common Mistakes:
Using function keyword to declare variables
Using var or let which are not Solidity keywords
Declaring variables inside functions as state variables
3. Consider this Solidity contract snippet:
contract Counter {
uint256 public count = 0;
function increment() public {
count += 1;
}
}
What will be the value of count after calling increment() three times?
medium
A. 1
B. 3
C. 0
D. Error: count is not accessible
Solution
Step 1: Understand initial value and increment
The state variable count starts at 0 and increases by 1 each time increment() is called.
Step 2: Calculate after three calls
After three calls, count = 0 + 1 + 1 + 1 = 3.
Final Answer:
3 -> Option B
Quick Check:
0 + 3 increments = 3 [OK]
Hint: Add 1 to count for each increment call [OK]
Common Mistakes:
Forgetting count starts at 0
Assuming count resets after each call
Thinking count is private and inaccessible
4. Identify the error in this Solidity contract:
contract Example {
uint256 count;
function setCount(uint256 _count) public {
uint256 count = _count;
}
}
medium
A. Local variable shadows state variable inside function
B. State variable count is not declared
C. Function setCount has wrong return type
D. Missing visibility for state variable count
Solution
Step 1: Analyze variable declarations
The state variable count is declared correctly. Inside setCount, a new local variable count is declared, which hides the state variable.
Step 2: Understand shadowing effect
Assigning to local count does not change the state variable. This is a common mistake called variable shadowing.
Final Answer:
Local variable shadows state variable inside function -> Option A
Quick Check:
Local variable shadows state variable = bug [OK]
Hint: Avoid declaring local variable with same name as state variable [OK]
Common Mistakes:
Thinking state variable is missing
Ignoring variable shadowing
Assuming function needs return type
5. You want to create a smart contract that stores a user's name permanently and allows updating it. Which of these is the best way to declare the state variable and update it?
hard
A. Declare string memory name; as state variable
B. Declare string name; inside the update function and assign new value there
C. Use a local variable string name; in constructor only
D. Declare string public name; as state variable and update it in a public function
Solution
Step 1: Understand state variable declaration for permanent storage
State variables must be declared outside functions with proper type and visibility, e.g., string public name;.
Step 2: Update state variable in a public function
To change the stored name, write a public function that assigns a new value to the state variable.
Final Answer:
Declare string public name; as state variable and update it in a public function -> Option D
Quick Check:
State variable + public update function = correct pattern [OK]
Hint: Declare state variable outside, update inside public function [OK]
Common Mistakes:
Declaring variables inside functions only
Using memory keyword for state variables
Not providing visibility for state variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.