Modifiers
In Solidity, function modifiers are keywords or reusable code snippets that change the behavior of functions. They help improve code readability, reduce repetition, and enforce rules such as access control or payment requirements.
The most common built-in function modifiers are view, pure, and payable. Additionally, developers can create custom modifiers to implement specific logic.
1. View Modifier
The view modifier specifies that a function can read state variables but cannot modify them. It is typically used for getter functions that retrieve information without altering the blockchain state.
Example (Checking balance):
pragma solidity ^0.8.0;
contract ViewExample {
uint256 public balance = 1000;
function getBalance() public view returns (uint256) {
return balance; // can read state but not change it
}
}
getBalance() is marked view because it only reads the value of balance without modifying it.
2. Pure Modifier
The pure modifier is used when a function neither reads nor writes to the blockchain state. Such functions rely only on input parameters or perform calculations.
Example (Mathematical calculation):
pragma solidity ^0.8.0;
contract PureExample {
function multiply(uint256 a, uint256 b) public pure returns (uint256) {
return a * b; // depends only on inputs
}
}
multiply() is pure because it does not interact with state variables at all.
3. Payable Modifier
The payable modifier allows a function to receive Ether. Without it, sending Ether to a function will fail. It is commonly used in donation systems, marketplaces, and ICO contracts.
Example (Receiving Ether):
pragma solidity ^0.8.0;
contract PayableExample {
address public owner;
constructor() {
owner = msg.sender;
}
// Function marked payable to accept Ether
function deposit() public payable {}
// Retrieve total contract balance
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
The deposit() function is payable, so users can send Ether along with the transaction.
4. Custom Modifiers
Custom modifiers are user-defined and allow developers to enforce rules and restrictions before executing a function. They are often used for access control (onlyOwner), validations, or pre-conditions.
Example (Access Control Modifier):
pragma solidity ^0.8.0;
contract CustomModifierExample {
address public owner;
constructor() {
owner = msg.sender;
}
// Custom modifier
modifier onlyOwner() {
require(msg.sender == owner, "Not the contract owner!");
_;
}
// Function restricted to only owner
function withdraw() public onlyOwner {
payable(owner).transfer(address(this).balance);
}
// Allow anyone to deposit Ether
function deposit() public payable {}
}
In this example:
- onlyOwner ensures that only the contract owner can call withdraw().
- The _ symbol tells Solidity where the body of the modified function should be inserted.
Summary
| Modifier Type | Purpose | Example Use Case |
|---|---|---|
| view | Reads state, no changes | Get balance, fetch details |
| pure | No state interaction | Math utilities, string operations |
| payable | Accepts Ether | Donations, payments |
| custom | Reusable logic | Access control, validations |
Continue Learning
Explore more topics in Solidity or browse other tutorials.