Modifier
In Solidity, modifiers are special type of functions that helps us add some extra functionality to the existing functions. You can define a modifier by using keyword ‘modifier’ followed by its name, and parameters it may take.
- Modifiers helps restrict access, add additional conditions to the existing functions without rewriting the entire function, saving extra lines of code.
- You can apply multiple modifiers to a function and pass parameters to the modifiers.
Example:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Counter {
uint256 public count = 0;
address public owner;
// Modifier definition
modifier onlyOwner {
require(msg.sender == owner, "Only owner allowed");
_;
}
// Modifier definition
modifier valueGreaterThanZero(uint number) {
require(number > 0);
_;
}
constructor() {
owner = msg.sender;
}
function increment() public onlyOwner {
count++;
}
function customIncrement(uint256 digit) external onlyOwner valueGreaterThanZero(digit) {
count += digit;
}
}
Output:
Initially, before calling the customIncrement method:
After calling the customIncrement method:
Unlock the Power of Solidity: Exploring the Essential Keywords for Smart Contract Development
Solidity is a smart contract programming language built solely to develop smart contracts that can be deployed on blockchains. C++ and JavaScript inspired it, an Object Oriented and High-Level language (HLL), that helps developers define the behaviour and rules for a Decentralized Application (Dapp). Smart contract facilitates autonomous, secure, and efficient transaction execution on the blockchain without needing any intermediary (self-executable) and solidity provides the base framework for achieving the same.
It is essential to understand the common keywords used in solidity to solidify your core understanding of the language so in this article, we’ll pin down some prominent keywords that help us develop these smart contracts by embracing the true power of solidity language.
Table of Content
- Contract
- Constructor
- Data Types
- Delete
- Enum
- Function
- Interface
- Import
- Struct
- Visiblity Specifiers
- Exception Handling
- Pragma Directive
- License Defintion
- Modifier
- Data Location
- Global Variables
- Fallback Function
- SelfDestruct
- ‘assembly’ and ‘inline assembly’