Function modifiers help change how functions work without changing their code. They add extra checks or actions before or after a function runs.
To check if a user has permission before running a function.
To make sure a function runs only once or under certain conditions.
To add logging or tracking around function calls.
To reuse common code like validation in many functions.
To protect functions from being called by unauthorized users.
Syntax
Blockchain / Solidity
modifier modifierName() {
// code to run before function
_; // placeholder for the function body
// code to run after function
}
function someFunction() modifierName() public {
// function code
}
The underscore _; is where the original function's code runs.
Modifiers can run code before and/or after the function.
Examples
This modifier checks if the caller is the owner before running the function.
This contract uses a modifier onlyOwner to allow only the owner to increase the count.
Blockchain / Solidity
pragma solidity ^0.8.0;
contract SimpleContract {
address public owner;
uint public count;
constructor() {
owner = msg.sender;
count = 0;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not owner");
_;
}
function increment() public onlyOwner {
count += 1;
}
function getCount() public view returns (uint) {
return count;
}
}
OutputSuccess
Important Notes
Modifiers help keep your code clean by reusing common checks.
Always place _; in the modifier where you want the function code to run.
Modifiers can take parameters to customize their behavior.
Summary
Function modifiers add extra rules or actions around functions.
Use _; inside modifiers to run the original function code.
Modifiers help keep your contract safe and organized.
Practice
(1/5)
1. What is the main purpose of a function modifier in blockchain smart contracts?
easy
A. To change the return type of a function
B. To add extra rules or checks around a function
C. To rename a function
D. To delete a function from the contract
Solution
Step 1: Understand function modifiers
Function modifiers are used to add extra rules or steps before or after a function runs.
Step 2: Compare options
Only To add extra rules or checks around a function correctly describes adding extra rules or checks around a function.
Final Answer:
To add extra rules or checks around a function -> Option B
Quick Check:
Function modifiers add rules [OK]
Hint: Modifiers add rules or checks around functions [OK]
Common Mistakes:
Thinking modifiers rename or delete functions
Confusing modifiers with return type changes
Believing modifiers execute functions themselves
2. Which of the following is the correct syntax to define a function modifier in Solidity?
easy
A. modifier onlyOwner() => { require(msg.sender == owner); _; }
B. modifier onlyOwner() : { require(msg.sender == owner); _; }
C. modifier onlyOwner() -> { require(msg.sender == owner); _; }
D. modifier onlyOwner() { require(msg.sender == owner); _; }
Solution
Step 1: Recall Solidity modifier syntax
Modifiers use the keyword 'modifier', name, parentheses, and curly braces with '_;' inside.
Step 2: Identify correct syntax
modifier onlyOwner() { require(msg.sender == owner); _; } matches the correct Solidity syntax with braces and '_;'. Others use invalid symbols like '=>', '->', or ':'.
D. Modifier name should be 'OnlyOwner' with capital O
Solution
Step 1: Check modifier syntax
The modifier has '_', but it misses the semicolon '_;' which is required to mark function execution point.
Step 2: Verify other parts
Function visibility 'public' is correct, owner can be uninitialized but not a syntax error, and modifier names are case-sensitive but not required to be capitalized.
Final Answer:
Missing semicolon after '_' in modifier -> Option A
Quick Check:
Modifier must have '_;' not just '_' [OK]
Hint: Modifier must end '_' with semicolon ';' [OK]
5. You want to create a modifier that allows a function to run only if the contract is not paused. The contract has a boolean paused. Which modifier code correctly implements this logic?
hard
A. modifier whenNotPaused() { require(!paused, "Paused"); _; }
B. modifier whenNotPaused() { if(paused) { revert("Paused"); } }
C. modifier whenNotPaused() { require(paused, "Paused"); _; }
D. modifier whenNotPaused() { if(!paused) { _; } }
Solution
Step 1: Understand the paused logic
The function should run only if paused is false, so require(!paused) ensures this.
Step 2: Check modifier completeness
modifier whenNotPaused() { require(!paused, "Paused"); _; } uses require with negation and includes '_;' to run the function. modifier whenNotPaused() { if(paused) { revert("Paused"); } } lacks '_;', so function never runs. modifier whenNotPaused() { require(paused, "Paused"); _; } requires paused to be true, which is opposite. modifier whenNotPaused() { if(!paused) { _; } } runs function only if not paused but lacks else handling and is less clear.
Final Answer:
modifier whenNotPaused() { require(!paused, "Paused"); _; } -> Option A
Quick Check:
Require !paused with '_;' runs function [OK]
Hint: Use require(!paused) and include '_;' to run function [OK]
Common Mistakes:
Reversing paused condition in require
Omitting '_;' causing function not to run
Using if without else or revert
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.