In
If the pure function is doing any of the following, the compiler will consider them as reading state variables and will throw a warning:
If the pure function is doing any of the following, the compiler will consider them as modifying state variables and will throw a warning.
selfdestruct
Pure functions are allowed to use the
revert()
andrequire()
functions.
function <function-name>() <access-modifier> pure returns() {
// function body
}
In the code snippet below, we will see how to create a pure function in Solidity.
pragma solidity ^0.5.0;contract Example {// creating a pure function// it returns sum of two numbers that are passed as parameterfunction getSum(uint number1, uint number2) public pure returns(uint) {uint sum = number1 + number2;return sum;}}
Example
.getSum()
.getSum()
function accepts two parameters number1
and number2
, and calculates their sum.Since the function getSum()
doesn’t read or modify any state variables, it is considered a Pure
function.