typestar

view and pure in Solidity

view reads state, pure touches none; offchain both are free.

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

// view reads state, pure touches none, neither costs gas offchain.
contract HarvestMath {
    uint256 public bushels = 120;

    function stored() external view returns (uint256) {
        return bushels;
    }

    function crates(uint256 perCrate) external view returns (uint256) {
        return bushels / perCrate;
    }

    function juiceLiters(uint256 b) external pure returns (uint256) {
        return b * 35;
    }
}

How it works

  1. stored() is view: it reads bushels but cannot write.
  2. crates mixes state with an argument, still view.
  3. juiceLiters is pure — arguments in, answer out.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
433
Tokens
69
Three-star pace
60 tpm

At the three-star pace of 60 tokens a minute, this run takes about 69 seconds.

Type this snippet

Step 3 of 4 in Contracts, step 3 of 28 in Language basics.

← Previous Next →