Quiz Entry - updated: 2026.07.30
What are functions in Solidity, and what happens when you call one?
Self-contained blocks of code, declared with the function keyword, that run a set of instructions when called — the same idea as methods in Java, Python, or JavaScript.
function store(uint256 _favoriteNumber) public {
favoriteNumber = _favoriteNumber;
}
A function can take parameters (here _favoriteNumber) and optionally return values. Calling it executes its body. Whether a call costs gas depends on what it does:
- A function that changes state (like
storewriting tofavoriteNumber) must be sent as a transaction and costs gas. - A function that only reads costs no gas when called on its own (see
view/pure).
The _ prefix on _favoriteNumber is only a naming convention to distinguish a parameter from a similarly named state variable — it has no special meaning to the compiler.
Go deeper:
Solidity docs: functions — declarations, parameters, return values, and calls.