Pure and view Functions

Published by Mario Oettler on

Sometimes, functions do not modify the state or read from it. In order to mark those functions, the keywords view and pure are used.


If a function is marked as view, it doesn’t modify the state but it can read from the state.

The following statements modify the state:

  1. Writing to state variables,
  2. emitting events,
  3. creating other contracts,
  4. sending Ether,
  5. calling functions that are not marked pure or view,
  6. using low-level-calls,
  7. invoking selfdestruct,
  8. certain opcodes from inline assembly.


If a function is marked pure, it neither reads from the state nor modifies it.

The following statements are considered as reading from the state:

  1. reading from state variables,
  2. accessing the balance of an address,
  3. accessing any of the members of block, tx, msg (except msg.sig and msg.data),
  4. calling any function that is not pure,
  5. certain opcodes from inline assembly.

Code Sample

The following code sample shows how to use pure and view.

pragma solidity 0.8.20;

contract pureAndview{
    string greetings = "Hello World";

    function myViewFunction() view public returns (string memory){
        return greetings;

    function myPureFunction(uint256 _a, uint256 _b) pure public returns (uint256){
        return _a+ _b;