Solidity theorytheory 0/50 · 0%
Functions · easy

5. view, pure and payable

State-mutability modifiers and what they promise.

`view` promises not to modify state, `pure` promises not to read or modify it, and `payable` allows the function to receive ETH.

A `view` or `pure` call made off chain through `eth_call` costs the caller no gas, because no transaction is mined.

function balanceOf(address a) external view returns (uint256) { return bal[a]; }
function add(uint256 a, uint256 b) external pure returns (uint256) { return a + b; }
function deposit() external payable { bal[msg.sender] += msg.value; }

Without `payable`, sending ETH to a function reverts.

Check your understanding

  1. 1. Which modifier allows reading but not writing state?

  2. 2. What happens if ETH is sent to a non-payable function?

  3. 3. Does an off-chain `view` call cost gas?