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
This example begins with count zero and a request to call increment(). No real contract is deployed.
This example begins with count zero and a request to call increment(). No real contract is deployed.Execution follows the code’s input and permission rules. A website button is not the access-control mechanism.The function reads zero, calculates zero plus one, and writes one to persistent storage.The accepted state contains count one. Later reads can inspect it. Events are emitted only if the code implements them.
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?
Pure functions can use permitted local inputs without reading contract state.