Unit 4 – Structure of Blockchain · 46
Functions
Functions define actions a Solidity contract can perform. Their visibility, state mutability and access controls determine who can call them, whether they can alter storage and how their results are returned.
Animated diagram
A function is a named button with defined inputs, actions and outputs; access rules decide who may press it
The idea in one line
Make the invisible feel visible.
Functions define actions a Solidity contract can perform. Their visibility, state mutability and access controls determine who can call them, whether they can alter storage and how their results are returned.
Real-life lens
Think of it like this
A function is a named button with defined inputs, actions and outputs; access rules decide who may press it.
Under the hood
How it works, step by step
Start with the idea
Functions define actions a Solidity contract can perform. Their visibility, state mutability and access controls determine who can call them, whether they can alter storage and how their results are returned.
Follow the example
An `addOne()` function can increment a stored counter, while a `view` function can return the current count without changing state.
Notice the benefit
Functions organize reusable contract behavior and make interactions explicit.
Check the limitation
A missing access check or incorrect state change can expose valuable actions to unintended callers.
Make it concrete
Functions in practice
Functions in practice
An `addOne()` function can increment a stored counter, while a `view` function can return the current count without changing state.
Key point: A missing access check or incorrect state change can expose valuable actions to unintended callers.
Balanced view
Advantages & disadvantages
Advantages
- Functions organize reusable contract behavior and make interactions explicit.
Disadvantages
- A missing access check or incorrect state change can expose valuable actions to unintended callers.
Keep these close
Takeaways & common traps
What to remember
- Functions define actions a Solidity contract can perform. Their visibility, state mutability and access controls determine who can call them, whether they can alter storage and how their results are returned.
- Functions organize reusable contract behavior and make interactions explicit.
- A missing access check or incorrect state change can expose valuable actions to unintended callers.
Watch out for
- Do not assume functions guarantees more than its design can prove.
- Do not ignore the trade-off: A missing access check or incorrect state change can expose valuable actions to unintended callers.
Quick check
Questions people ask
Why is functions useful?
Functions organize reusable contract behavior and make interactions explicit.
What is an important limitation?
A missing access check or incorrect state change can expose valuable actions to unintended callers.