What are Solidity modifiers?

Answer

Modifiers in Solidity are reusable code patterns that change the behavior of functions — typically for access control and validation. They use the _; placeholder to indicate where the function body executes. Example: modifier onlyOwner() { require(msg.sender == owner, "Not owner"); _; } modifier nonReentrant() { require(!locked, "Reentrant call"); locked = true; _; locked = false; } function withdraw() external onlyOwner nonReentrant { payable(msg.sender).transfer(address(this).balance); }. Common modifier patterns: (1) Access controlonlyOwner, onlyAdmin, onlyRole(bytes32 role); (2) State checkswhenNotPaused, onlyWhenActive; (3) Reentrancy guardnonReentrant prevents reentrancy attacks; (4) Input validation — check parameters before function body. Modifiers are inlined at compile time. Multiple modifiers can be chained: function adminAction() external onlyOwner whenNotPaused { ... }. The OpenZeppelin Contracts library provides battle-tested modifiers via Ownable, AccessControl, Pausable, and ReentrancyGuard.