0
0
Blockchain / Solidityprogramming~10 mins

State variables in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - State variables
Contract deployed
State variables created in storage
Function called
Read or update state variables
State variables persist after function ends
Next function call uses updated state
State variables are stored on the blockchain and keep their values between function calls in a smart contract.
Execution Sample
Blockchain / Solidity
contract Example {
  uint count;
  function increment() public {
    count = count + 1;
  }
  function getCount() public view returns (uint) {
    return count;
  }
}
This contract has a state variable 'count' that increments each time 'increment' is called and can be read with 'getCount'.
Execution Table
StepActionState Variable 'count'Output
1Contract deployed0 (default)None
2Call increment()count = 0 + 1 = 1None
3Call getCount()count = 1Returns 1
4Call increment()count = 1 + 1 = 2None
5Call getCount()count = 2Returns 2
6End of callscount = 2State persists on blockchain
💡 Execution stops after calls; state variable 'count' keeps its last value on blockchain.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
count0122
Key Moments - 2 Insights
Why does the value of 'count' not reset to 0 after each function call?
Because 'count' is a state variable stored on the blockchain, it keeps its value between calls as shown in steps 2, 3, 4, and 5 in the execution_table.
What happens if we call 'getCount' before any 'increment' calls?
The value returned would be 0, the default initial value of the state variable 'count', as shown in step 1 where 'count' starts at 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after the first call to increment()?
A0
B1
C2
DUndefined
💡 Hint
Check Step 2 in the execution_table where 'count' is updated to 1.
At which step does the 'getCount()' function return the value 2?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look at Step 5 in the execution_table where 'getCount()' returns 2.
If we never call 'increment()', what will 'getCount()' return?
AError
B1
C0
DUndefined
💡 Hint
Refer to Step 1 in the execution_table where 'count' starts at 0.
Concept Snapshot
State variables store data permanently on the blockchain.
They keep their values between function calls.
Declared inside contracts but outside functions.
Updated by functions and persist after execution.
Used to track contract state like counters or balances.
Full Transcript
State variables in blockchain smart contracts are special variables that store data permanently on the blockchain. When a contract is deployed, these variables are created in the blockchain's storage with default values. Functions in the contract can read or update these state variables. Unlike local variables, state variables keep their values between function calls, so changes persist on the blockchain. For example, a 'count' variable can be incremented by calling a function, and its updated value remains stored for future calls. This behavior is shown step-by-step in the execution table, where 'count' starts at 0, increments to 1, then 2, and can be read anytime. This persistence is key to tracking contract state like balances or counters.