Modifier with Arguments

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

Syntax:

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

Below is the solidity program to demonstrate function modifier with arguments:

Solidity




// Solidity program to demonstrate
// modifier with argument
//SPDX-License-Identifier:GPL-3.0
pragma solidity >=0.4.22 <0.7.0;
contract modifierWithArg {
      
  struct employee
  {
    uint emp_id;
    string emp_name;
    uint age;
   }
    
    modifier isExperienced(uint exp)
    {
        if(exp >= 5)
            _;
        else
            revert("Must have a minimum of 5 years of experience");
    }
  employee e;
 function enterDetails (uint _empid, string memory _empname, 
                        uint _empage) public isExperienced(7) {
   e.emp_id = _empid;
   e.emp_name = _empname;
   e.age = _empage;
 }
}


Output: Here, the user with a minimum of 5 years of experience is only allowed to make entry. This is ensured with the help of isExperienced modifier. Because the experience in the example is hard-coded as 7, the details can be entered otherwise for a value less than 5, a message will be displayed “Must have a minimum of 5 years of experience”.

 

Explanation:

1. Modifier creation:

modifier isExperienced(uint exp)
{
   if(exp >= 5)
       _;
   else
      revert(“Must have a minimum of 5 years of experience”);
}

The above code creates the modifier named isExperienced which takes the experience of the user as the argument. This modifier allows the function to execute only if the experience is >= 5. If the user has less than 5 years of experience then it prompts the message “Must have a minimum of 5 years of experience”.

2. Modifier invocation: The modifier can be invoked by passing the value for the experience.  In this example, as shown below, the experience is hard coded as 7 years. Hence the details will be recorded.

function enterDetails (uint _empid, string memory _empname, 
                                   uint _empage) public isExperienced(7) {
  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