Functions and Modifiers
Functions and Modifiers
Declaring a Function
// SPDX-License-Identifier: GPL-3.0
/// @notice For now, this contract just show how to calculate the sum of two numbers
contract Test {
return sum;
}
}
Output :
In the above example, you must have seen the usage of “pure”.
pure in a function ensures that they will not modify the state of the
function. The following statements if present in the function will
modify the state of the function and the compiler will throw a
warning.
Modifying state variables.
Emitting events.
Creating other contracts.
Using self-destruct.
Sending Ether via calls.
Calling any function which is not marked pure or view.
Using inline assembly containing certain opcodes.
Using low-level calls.
Function Calling
Solidity
// SPDX-License-Identifier: GPL-3.0
/// @notice For now, this contract just show how to call one function into the
another function
contract Test {
//Defining public pure sqrt function
_num = _num ** 2;
return _num;
Output :
Return Statements
Output :
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.
Function modifiers are created by defining them and invoking the same in the required
function.
Syntax:
modifier modifier_name
{
// 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.
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:
//SPDX-License-Identifier:GPL-3.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;
e.emp_id = _empid;
e.emp_name = _empname;
e.age = _empage;
Output:
Explanation:
Solidity
// Solidity program to demonstrate
//SPDX-License-Identifier:GPL-3.0
contract modifierWithArg {
struct employee
uint emp_id;
string emp_name;
uint age;
if(exp >= 5)
_;
else
employee e;
e.emp_id = _empid;
e.emp_name = _empname;
e.age = _empage;
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;
}
Multiple Modifiers to Function
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.
Syntax:
modifier modifier_name(datatype arg_name)
{
// action to be taken
}
function func_name(arg1, arg2, …) public modifier1 modifier2
{
// function definition
}
Below is the solidity program to implement multiple modifiers to
function:
Solidity
// Solidity program to demonstrate
// multiple modifier to function
//SPDX-License-Identifier:GPL-3.0
contract multiplemodifier {
address admin;
struct employee
uint emp_id;
string emp_name;
uint age;
constructor() public
admin = msg.sender;
modifier isAdmin
require(admin == msg.sender);
_;
if(exp>=5)
_;
else
employee e;
function enterDetails (uint _empid, string memory _empname,
e.emp_id = _empid;
e.emp_name = _empname;
e.age = _empage;
Output:
Solidity
// Solidity program to demonstrate
//SPDX-License-Identifier:GPL-3.0
contract modifierOverride {
if(exp >= 5)
_;
else
struct employee
uint emp_id;
string emp_name;
uint age;
}
employee e;
if(exp >= 5)
_;
else
e.emp_id = _empid;
e.emp_name = _empname;
e.age = _empage;
Output:
Solidity
//SPDX-License-Identifier:GPL-3.0
contract modifierWithEnum {
struct employee
uint emp_id;
string emp_name;
Status s;
Status s1;
require(s1==_entry);
_;
employee e;
function enterDetails (uint _eno, string memory _ename, Status _s) public isValid(_s)
e.emp_id=_eno;
e.emp_name=_ename;
e.s=_s;
Output: