A function modifier in Solidity is reusable code that runs before and, optionally, after a function body, and it is most often used to check a condition such as ownership before the function executes. You declare it with the modifier keyword, mark the point where the function body runs with _;, and attach it to a function by naming it after the visibility. The solidity modifiers feature keeps access checks in one place instead of repeating them in every function.
This guide uses Solidity 0.8.x and assumes you know functions, visibility and require.
A modifier contains checks and a placeholder _;. When a function with the modifier is called, the code before _; runs first, then the function body takes the place of _;, then any code after it runs.
If msg.sender is not the owner, require reverts the whole call and setFee never runs. Every function that needs the check only adds onlyOwner to its header, so a change in the rule happens in one place.
A modifier can take arguments, which makes it flexible. The arguments can come from the function's own parameters or be constants.
Here join is protected by two conditions. The 0.1 ether literal is a constant, and openedAt is read from storage when the modifier runs.
Code placed after _; runs once the function body finishes. The classic use is a reentrancy guard.
The flag is set before the body and cleared after it, so a second call during the external call reverts. In production, prefer the audited ReentrancyGuard contract from OpenZeppelin rather than writing your own, and also follow the checks-effects-interactions order shown above, where the balance is updated before the external call.
A function can list many modifiers, separated by spaces. They run from left to right, and each one's _; runs the next modifier, and finally the body.
Order matters when one check is cheaper or when a later modifier depends on an earlier one. Put cheap access checks first so unauthorised callers fail early and waste little gas.
A return inside a modifier only leaves that modifier. Since Solidity 0.8.0, the function body is skipped if the modifier never reaches _;, but the surrounding code still finishes. Because an implicit return inside the body ends only the body, code after _; still runs. A modifier that never reaches _; and does not revert makes the function silently do nothing, which is almost always a bug, so use require or revert to reject calls.
Modifiers are inherited. A child contract can use the parent's modifiers directly, and a parent may mark a modifier virtual so a child can replace it with override.
Modifier code is inlined into every function that uses it, so a big modifier increases bytecode size. For checks with long logic, put the work in an internal function and call it from a short modifier.
This is the style OpenZeppelin uses in its Ownable contract, and it keeps the bytecode smaller.
_; marks where the modified function's body is inserted. Code before it runs first and code after it runs afterwards.
Yes. A modifier can read and write state, as the reentrancy lock does. Any revert inside it undoes the whole call.
Yes. You pass them in the modifier call, such as minAmount(0.1 ether), and can use the function's own parameters in that expression.
No. require is a single check inside code. A modifier packages checks so you can reuse them across functions.
A modifier wraps a function with reusable checks that run before and sometimes after its body, with _; marking the body position. Use modifiers such as onlyOwner for access control and guards, give them parameters when needed, and remember that several modifiers run left to right. Keep large logic in internal functions that short modifiers call, to avoid bloating the bytecode.