Michael Etokakpan
Michael Tech Blog

Follow

Michael Tech Blog

Follow
Solidity: View and Pure Functions

Photo by Shahadat Rahman on Unsplash

Solidity: View and Pure Functions

Michael Etokakpan's photo
Michael Etokakpan
·Jan 12, 2023·

1 min read

Getter functions can be declared view or pure.

View function declares that no state will be changed.

Pure function declares that no state variable will be changed or read.

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.3;

contract ViewAndPure {
    uint public x = 1;

    // Promise not to modify the state.
    function addToX(uint y) public view returns (uint) {
        return x + y;
    }

    // Promise not to modify or read from the state.
    function add(uint i, uint j) public pure returns (uint) {
        return i + j;
    }
}
 
Share this