Function Modifiers

Learn how to use function and multiple modifiers.

Modifiers can be used to change the behavior of functions in a declarative way. They can be considered as helper code that changes the behavior of functions. We can have modifiers that check access or the existence of certain data before the main function code is run.

Declaring modifiers

To declare modifiers, we use the modifier keyword, followed by the name and then the body of the modifier. The modifier body must contain at least one _; line. This line specifies the point where we wish to inject the body of the function called with the modifier.

Note: Modifiers do not return any value.

When using a defined modifier in a function, we place it just after the mutability type identifier. Here’s an example of a modifier:

Get hands-on with 1200+ tech skills courses.