Solidity: How to use the pure getter and view functions?

Bruno Delb
Crypto DevOps
Published in
2 min readJul 5, 2022

--

Solidity provides two particular types of getter functions: view and pure.

The view functions

view allows the state to be read but does not allow the state to be modified:

uint public a = 1;function viewFunction(uint b) public view returns (uint) {
return a + b;
}

Let’s compile and deploy the contract. Then call the viewFunction() function:

The result is 3 = 1 + 2.

The view functions therefore ensure that they will not modify the state. The following statements modify the state:

  • Modify a state variable,
  • Emit an event,
  • Create a contract,
  • Use self-destruct,
  • Send Ether via a call,
  • Call a function that is not marked view or pure,
  • Use a low level call,
  • Use an inline assembly containing some opcodes.

Getter methods are by default view functions.

The pure functions

pure forbids the reading or modification of the state :

function pureFunction(uint x, uint y) public pure returns (uint) {
return x + y;
}

Let’s compile and deploy the contract. Then call the pureFunction() function:

--

--

Bruno Delb
Crypto DevOps

Blockchains, DevOps, Agile Coaching, development, testing, Cloud, Management 3.0, ITIL. It defines me.