0
0
Blockchain / Solidityprogramming~5 mins

Function modifiers in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a function modifier in Solidity?
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:<br>modifier onlyOwner(address _addr) { require(msg.sender == _addr); _; }<br>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
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
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() { ... }
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
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
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.