What is the difference between pure and view modifiers in solidity?

Viewed 4877

The code results in same output.

pragma solidity ^0.5.0;
contract mycontract
{

   function add(uint c, uint d) public pure returns(uint)
  { uint e=c+d;


   return e;

  } 
   function add(uint j, uint k) public view returns(uint)
  { uint f=j+k;


   return f;

  } 

}
2 Answers
  • view indicates that the function will not alter the storage state in any way. But it allows you to "view" it

  • pure is even more strict, indicating that it will not even read the storage state.

    A pure function is a function which given the same input, always returns the same output. But the state of the contract keeps changing as users interact with it. So if you pass a state variable as an argument to the function, since the state is changing, that function will not be pure. That's why pure functions cannot access to state.

    Also inside pure functions you cannot

    • use address(this).balance

    • call other functions except pure functions

if you call view or pure functions externally, you do not pay a gas fee. However, they do cost gas if called internally by another function.

pure does not view nor modify state. i.e. it can only use what is provided to it to run. view cannot modify state, but can look it up.

Related