Modifiers without Argument

A modifier may not have arguments. If the modifier does not have an argument then parentheses () can be omitted.

Syntax:

modifier  modifier_name ( ) 
{
   // action to be taken
}

OR

modifier  modifier_name 
{
  // action to be taken
}

Here, the modifier is the keyword and modifier_name is the modifier name.

Below is the solidity program to demonstrate the modifier without an argument:

Solidity




// Solidity program to demonstrate
// modifier with argument
//SPDX-License-Identifier:GPL-3.0
pragma solidity >=0.4.22 <0.7.0;
contract modifierWithoutArg {
   address admin; 
  struct employee
  {
    uint emp_id;
    string emp_name;
    uint age;
   }
     
   constructor() public 
   {
     admin = msg.sender;
   }
      
    modifier isAdmin
    {
        require(admin == msg.sender);
        _;
    }
  employee e;
 function enterDetails (uint _empid, string memory _empname, 
                        uint _empage) public isAdmin {
   e.emp_id = _empid;
   e.emp_name = _empname;
   e.age = _empage;
 }
}


Output:

 

Explanation:

1. Address initialization: In the above code, constructor is used for initializing the address for admin. In Solidity language, the address of the contract creator will be saved in msg.sender. Hence, the address present in this variable is assigned to admin.

constructor() public 
 {
      admin = msg.sender;
 }

2. Modifier creation: The modifier named isAdmin is created using the following code. This modifier verifies whether the address of the person entering the detail is the address present in admin variable.

modifier isAdmin
{
   require(admin == msg.sender);
    _;
}

3. Function definition: Once the modifier is defined, the modifier is included in the function wherever the above constraint needs to be imposed. In this example, the function enterDetails includes the modifier isAdmin which enables the user to enter the employee details only if the entry is made from the admin address.

function enterDetails (uint _empid, string memory _empname, 
                                   uint _empage) public isAdmin {
  e.emp_id = _empid;
  e.emp_name = _empname;
  e.age = _empage;
}
 

Solidity – Function Modifiers

Function behavior can be changed using function modifiers. Function modifier can be used to automatically check the condition prior to executing the function. These can be created for many different use cases. Function modifier can be executed before or after the function executes its code.

  • The modifiers can be used when there is a need to verify the condition automatically before executing a particular function.
  • If the given condition is not satisfied, then the function will not get executed.

How to Create and Use Modifiers?

Function modifiers are created by defining them and invoking the same in the required function. 

Syntax:

modifier  modifier_name 
{
   // action to be taken
}

There are two variations of a function modifier: 

1. Function modifier with an argument:

modifier  modifier_name(unit arg)
{
   // action to be taken
}

2. Function modifier without argument:

modifier  modifier_name()
{
   // action to be taken
}

If the modifier does not have an argument then parentheses () can be omitted.

What is Merge Wildcard?

Consider the below function modifier:

modifier isAdmin
{
   require(msg.sender == admin);
   _;
}

The _; symbol is known as Merge Wildcard and this is replaced by the function definition during execution. 

  • In other words, after this wildcard has been used, the control is moved to the location where the appropriate function definition is located. 
  • This symbol is mandatory for all modifiers.
  • The modifier may contain this wildcard anywhere. 
  • When the wildcard is placed at the end of the modifier, the condition is verified and the appropriate function is executed if it is satisfied. 
  • When it is placed at the beginning, the appropriate function is executed first followed by the condition verification.

Similar Reads

Modifiers without Argument

A modifier may not have arguments. If the modifier does not have an argument then parentheses () can be omitted....

Modifier with Arguments

...

Multiple Modifiers to Function

Modifiers may accept an argument and it is included after the modifier name within parentheses ()....

Modifier Overriding

...

Modifier with enums

Multiple modifiers may be present in a function, and each of these conditions must be met in order for the function to be successfully executed.  To verify whether only the administrator with 5 years of experience is editing, two modifiers namely isAdmin and isExperienced are introduced. The function enterDetails will execute only if the user has administrator authorization and has a minimum of 5 years of experience....

Contact Us