A function modifier is a special code block that can be used to change the behavior of functions. It helps to add checks or logic before or after the function runs, like a gatekeeper.
Click to reveal answer
beginner
How do you apply a modifier to a function in Solidity?
You write the modifier name after the function declaration, like this: <code>function myFunc() public myModifier { ... }</code>. This means the modifier code runs when the function is called.
Click to reveal answer
intermediate
What does the _; symbol mean inside a modifier?
The _; is a placeholder that tells Solidity where to run the original function's code inside the modifier. Code before _; runs before the function, and code after runs after.
Click to reveal answer
beginner
Why use function modifiers in smart contracts?
Modifiers help keep code clean and safe by reusing common checks like access control or input validation. This avoids repeating the same code in many functions.
Click to reveal answer
intermediate
Can modifiers take arguments? Give an example.
Yes, modifiers can take arguments to customize their behavior. For example: modifier onlyOwner(address _addr) { require(msg.sender == _addr); _; } This modifier checks if the caller is the owner passed as argument.
Click to reveal answer
What is the main purpose of a function modifier in Solidity?
ATo create new functions automatically
BTo store data in the blockchain
CTo change how a function behaves by adding reusable code before or after it
DTo compile the smart contract
✗ Incorrect
Function modifiers add reusable code that runs before or after a function, changing its behavior.
In a modifier, what does the _; symbol do?
AIt marks where the original function's code will run
BIt ends the modifier
CIt declares a variable
DIt calls another function
✗ Incorrect
The _; symbol is a placeholder for the original function's code inside the modifier.
How do you apply a modifier named onlyOwner to a function withdraw?
Amodifier withdraw() onlyOwner { ... }
Bfunction withdraw() public onlyOwner { ... }
ConlyOwner function withdraw() { ... }
Dfunction onlyOwner withdraw() { ... }
✗ Incorrect
You add the modifier name after the function declaration to apply it.
Can function modifiers take parameters in Solidity?
AYes, to customize their checks
BNo, modifiers cannot have parameters
COnly if the function is private
DOnly for constructors
✗ Incorrect
Modifiers can take parameters to customize their behavior, like checking different addresses.
Why are function modifiers useful in smart contract development?
AThey encrypt data
BThey speed up the blockchain
CThey create new tokens
DThey help reuse code and improve security by adding checks
✗ Incorrect
Modifiers help reuse common code like access control, making contracts safer and cleaner.
Explain what a function modifier is and how it changes a function's behavior in Solidity.
Think of a modifier as a helper that wraps around a function.
You got /3 concepts.
Describe how you would use a function modifier to restrict access to only the contract owner.
Use require to check who calls the function.
You got /3 concepts.
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.