Skip to content

    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

    Declare type → Store value → Read or update → New state. Follow the flow to see how variables fits into a blockchain system.

    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.

    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

    01

    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.

    02

    Follow the example

    `uint256 public count;` declares a persistent counter that a contract function can increment.

    03

    Notice the benefit

    Named, typed variables make contract data easier to organize, validate and read.

    04

    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.