In this track: Write and read Solidity
advanced · 4 MIN READ

View and pure functions

State-access restrictions differ from whether execution happens inside a paid transaction.

LET’S PICTURE IT

A calculator can promise not to edit a notebook while still requiring work to produce its answer.

SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
A valid counter call, step by stepFunction requestincrement()No argumentsBefore executionstored count0A local teaching example with a defined starting stateIncoming callCheck function dispatchCheck coded conditionsRules passConditions passContinue executionA failed check can revert insteadCalculation0 + 1Result: 1Storage writecount = 1Persists if call succeedsCalculating a local value and storing it are separate stepsPersistent statecount1Later readGetter reports 1No new incrementState, logs and browser display have different roles

This example begins with count zero and a request to call increment(). No real contract is deployed.

Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.

View restricts state modification

A view function can read relevant state while promising not to modify it through that execution under language rules. Generated getters are common examples.

Pure has a narrower dependency set

A pure function does not read or write contract state and is subject to Solidity’s restrictions on environmental access. It can calculate from permitted inputs.

Neither label makes onchain work free

A local call simulation can avoid submitting a transaction. If the same function executes within a real transaction, its operations still consume resources.

Choose according to behavior

The example returns a calculation using only its inputs. If you change it to read a state variable, the compiler’s rules for pure no longer fit. Treat compiler diagnostics as evidence about the actual code.

Read the example

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

contract LearningExample {
    function add(uint256 a, uint256 b)
        external pure returns (uint256)
    {
        return a + b;
    }
}

Calling this locally and calling it inside a transaction have different fee contexts.

QUICK CHECK · NO PRESSURE

Can a pure function calculate from its input parameters?

Your progress stays in this browser.
Keep exploring the original referenceSolidity documentation ↗

Original explanations by EtherTools. Examples are educational and source-linked; this site is independent of the Ethereum Foundation. Our approach.