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
stored()isview: it readsbushelsbut cannot write.cratesmixes state with an argument, still view.juiceLitersispure— arguments in, answer out.
Keywords and builtins used here
contractexternalfunctionpublicreturnreturnsuint256
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.
Step 3 of 4 in Contracts, step 3 of 28 in Language basics.