Quiz Entry - updated: 2026.07.30
What is the classic "store / retrieve" pattern used to introduce Solidity state?
A minimal contract with one state variable, a store function that writes it (a paid transaction) and a retrieve view function that reads it (free) — the smallest example of on-chain persistent state.
contract SimpleStorage {
uint256 favoriteNumber; // starts at 0
function store(uint256 _n) public { // writes state -> costs gas
favoriteNumber = _n;
}
function retrieve() public view returns (uint256) { // reads -> free
return favoriteNumber;
}
}
It demonstrates the core split: store modifies the blockchain, so it must be a transaction and burns gas; retrieve only reads, so it is a free view call. Adding more work inside store (e.g. favoriteNumber = favoriteNumber + 1;) makes the transaction more computationally expensive and visibly raises its gas cost — a concrete demonstration that gas tracks how much work you do.
Go deeper:
ethereum.org: anatomy of a smart contract — state variables, functions, and events in one place.