0
0
Blockchain / Solidityprogramming~10 mins

Why functions define contract behavior in Blockchain / Solidity - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why functions define contract behavior
User calls function
Function receives input
Function executes logic
Function updates contract state or returns value
Contract behavior changes or outputs result
User sees outcome
Functions in a contract receive inputs, run code, update state or return values, defining how the contract behaves.
Execution Sample
Blockchain / Solidity
mapping(address => uint) balance;

function deposit(uint amount) public {
    balance[msg.sender] += amount;
}

function getBalance() public view returns (uint) {
    return balance[msg.sender];
}
A deposit function adds to the user's balance; getBalance returns the current balance.
Execution Table
StepActionInputState ChangeOutput
1User calls depositamount=10balance[user] = 0 + 10 = 10none
2User calls getBalancenonestate unchangedreturns 10
3User calls depositamount=5balance[user] = 10 + 5 = 15none
4User calls getBalancenonestate unchangedreturns 15
💡 Execution stops after returning balance or updating state as per function logic.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
balance[user]0101515
Key Moments - 2 Insights
Why does calling deposit change the contract state but getBalance does not?
Because deposit updates the balance variable (see steps 1 and 3 in execution_table), while getBalance only reads and returns the balance without changing it (steps 2 and 4).
What happens if a function is called with no input?
Functions like getBalance that don't need input can be called without parameters and still return data, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is balance[user] after step 3?
A10
B5
C15
D0
💡 Hint
Check the 'State Change' column at step 3 in the execution_table.
At which step does the contract state NOT change?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look for 'state unchanged' in the 'State Change' column in execution_table.
If deposit is called with amount=20 at step 3 instead, what would balance[user] be after?
A30
B20
C15
D10
💡 Hint
Add the new amount to the previous balance from variable_tracker.
Concept Snapshot
Functions in blockchain contracts define behavior by:
- Receiving inputs (parameters)
- Executing code to update contract state or return data
- Changing contract state only when modifying variables
- Returning values without state change for view functions
- Acting as the interface users interact with
Full Transcript
In blockchain contracts, functions are the building blocks that define how the contract behaves. When a user calls a function, it receives inputs and runs code. Some functions update the contract's state variables, changing stored data. Others just read data and return it without changing anything. For example, a deposit function adds to a user's balance, changing the state, while a getBalance function returns the current balance without changing state. This step-by-step execution shows how functions control contract behavior by managing inputs, state changes, and outputs.