Unit 4 – Structure of Blockchain · 44
Variables
Variables name values that a Solidity contract can use. State variables persist in contract storage, local variables exist during a function call, and constants or immutables express values that should not change after deployment or initialization.
Animated diagram
A variable is a labelled box; where the box is stored determines how long its value remains available
The idea in one line
Make the invisible feel visible.
Variables name values that a Solidity contract can use. State variables persist in contract storage, local variables exist during a function call, and constants or immutables express values that should not change after deployment or initialization.
Real-life lens
Think of it like this
A variable is a labelled box; where the box is stored determines how long its value remains available.
Under the hood
How it works, step by step
Start with the idea
Variables name values that a Solidity contract can use. State variables persist in contract storage, local variables exist during a function call, and constants or immutables express values that should not change after deployment or initialization.
Follow the example
`uint256 public count;` declares a persistent counter that a contract function can increment.
Notice the benefit
Named, typed variables make contract data easier to organize, validate and read.
Check the limitation
Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.
Make it concrete
Variables in practice
Variables in practice
`uint256 public count;` declares a persistent counter that a contract function can increment.
Key point: Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.
Balanced view
Advantages & disadvantages
Advantages
- Named, typed variables make contract data easier to organize, validate and read.
Disadvantages
- Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.
Keep these close
Takeaways & common traps
What to remember
- Variables name values that a Solidity contract can use. State variables persist in contract storage, local variables exist during a function call, and constants or immutables express values that should not change after deployment or initialization.
- Named, typed variables make contract data easier to organize, validate and read.
- Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.
Watch out for
- Do not assume variables guarantees more than its design can prove.
- Do not ignore the trade-off: Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.
Quick check
Questions people ask
Why is variables useful?
Named, typed variables make contract data easier to organize, validate and read.
What is an important limitation?
Persistent storage costs gas, and careless visibility or type choices can expose data or create logic errors.