0
0
Blockchain / Solidityprogramming~10 mins

Function modifiers in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Function modifiers
Call function
Enter modifier code
Run pre-function checks or code
Execute function body
Run post-function code if any
Return result or finish
When a function with a modifier is called, the modifier code runs first, then the function body, then any post-code in the modifier.
Execution Sample
Blockchain / Solidity
modifier onlyOwner() {
    require(msg.sender == owner, "Not owner");
    _;
}

function changeOwner(address newOwner) public onlyOwner {
    owner = newOwner;
}
This code uses a modifier to check if the caller is the owner before changing ownership.
Execution Table
StepActionEvaluationResult
1Call changeOwner with msg.sender = 0x123Enter onlyOwner modifierProceed to require check
2Check require(msg.sender == owner)msg.sender == 0x123, owner == 0xabcFalse, revert with 'Not owner'
3Call changeOwner with msg.sender = 0xabcEnter onlyOwner modifierProceed to require check
4Check require(msg.sender == owner)msg.sender == 0xabc, owner == 0xabcTrue, continue
5Execute function body: owner = newOwnernewOwner = 0xdefowner updated to 0xdef
6Function endsReturn successOwnership changed
💡 Execution stops if require condition fails; otherwise function completes normally.
Variable Tracker
VariableStartAfter Step 5Final
owner0xabc0xdef0xdef
msg.sendervariesvariesvaries
Key Moments - 2 Insights
Why does the function stop when msg.sender is not the owner?
Because the require statement in the modifier fails (see step 2 in execution_table), it reverts the transaction and stops execution before the function body runs.
What does the underscore (_) mean in the modifier?
The underscore marks where the function body runs inside the modifier. Before it runs, pre-checks happen; after it runs, post-code can run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2 when msg.sender is not the owner?
AThe function continues to execute
BThe require check passes
CThe transaction reverts with 'Not owner'
DThe owner variable is updated
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 2 in execution_table
At which step does the owner variable get updated?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for when owner changes
If the underscore (_) was removed from the modifier, what would happen?
AThe function body would not run
BThe require check would be skipped
CThe function would run twice
DThe modifier would have no effect
💡 Hint
Recall that underscore (_) indicates where the function body runs inside the modifier
Concept Snapshot
Function modifiers run code before and/or after a function.
Use '_' inside modifier to mark function body location.
Common for access control like 'onlyOwner'.
If modifier code fails (e.g. require), function stops.
Modifiers help reuse checks and logic cleanly.
Full Transcript
Function modifiers in blockchain let you run extra code before or after a function runs. When you call a function with a modifier, the modifier code runs first. It can check conditions like if the caller is the owner. The underscore (_) in the modifier shows where the function's main code runs. If a check fails, like require(msg.sender == owner), the function stops immediately and reverts. This helps keep your code safe and clean by reusing checks in many functions.