Function Modifier in Solidity

function modifier in solidityThe function modifier in Solidity can change the behaviour of the function it is attached to. This is similar to decorators in other programming languages like Python. 

The main use case of function modifiers is to check a condition before a function is executed, after it is executed or both. A very common use case is to do data validation within the modifier instead of the function to make the code reusable and less redundant. If the condition within the modifier is not met, an error is thrown and the function is not executed.

A modifier can be used for multiple functions to reduce the redundancy of the code. It is possible to define modifiers with or without arguments. And if the modifier is written without arguments, the parenthesis () are omitted. It is important to consider that modifiers are inheritable properties and can be overwritten by other contracts. 

write a function modifier

The example below shows a simple modifier without arguments named MyModifier. modifier MyModifier { // condition to be checked by modifier require(msg.sender == owner); // call the actual function _; } The underscore executes the actual code within the functions body. It is mandatory to have the underscore (_;) line within the modifiers body. The condition can be written before or after the underscore line, depending on if the condition has to be checked before or after the function is executed. The next step to make the modifier work is to attach it to the function. This is done by appending the modifiers name to the function declaration. The same modifier can be attached to multiple functions to reduce the code redundancy. // attach modifier MyModifier to function myFunction function myFunction() external MyModifier { // function code }

before, after or both?

A modifier can execute code before and after the main function is executed, or both. The most common way to use modifiers is to first execute the modifier code and then execute the main function. In any case it is important to use the underscore (_) within the modifier. modifier checkBefore { // first execute modifier code _; // then call the actual function } modifier checkAfter { _; // first call the actual function // then execute modifier code } modifier checkBoth { // execute modifier code _; // then call the function // finally execute more modifier code }

passing arguments to modifier

Modifiers accept arguments the same way functions do. The arguments have to be specified using the argument type and argument name. Then the arguments can be used within the modifier for checks. // modifier with arguments modifier MyModifier(uint _num) { require (_num > 10, "_num is not bigger than ten"); _; } function myFunction() external MyModifier(123) { // function code } In the example above the function is only executed, if the argument has a value bigger than 10. The argument passed to the modifier is 123 so the function will be executed.

multiple modifiers for a function

It is possible to apply multiple modifiers to a function. To do so list the modifiers separated by white spaces in the function definition. The priority of how the modifiers are checked is from left to right. In the example below the order of execution is modifier1, modifier2 and modifier3. // apply multiple modifiers to a function function myFunction() external modifier1 modifier2 modifier3 { // function code }



Click to jump to section