Skip to main content

Command Palette

Search for a command to run...

Solidity: View and Pure Functions

Updated
1 min read
Solidity: View and Pure Functions
M

Full Stack Developer and Cloud Engineer with 4+ years of hands-on experience designing, developing, and implementing applications and solutions using a range of technologies, platforms, and programming languages.

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;
    }
}

Starting with Solidity

Part 6 of 7

This series will introduce the basics of the Solidity language

Up next

Hello Solidity

Starting Solidity