What do the view and pure keywords mean, and why can calling them be free?
view = the function only reads state and cannot modify it; pure = it neither reads nor writes state. Both are free to call externally because reading the blockchain costs no gas — but they DO cost gas when a state-changing function calls them.
function retrieve() public view returns (uint256) {
return favoriteNumber; // reads state — view
}
function add(uint256 a, uint256 b) public pure returns (uint256) {
return a + b; // touches no state — pure
}
Gas is only paid to change the blockchain. A view/pure call made directly by a user just reads (or computes), changing nothing, so it is free — in Remix these are blue buttons and produce no transaction hash. The subtlety: if a state-changing function internally calls a view/pure function, that reading now happens as part of a paid transaction, so it adds to that transaction's gas cost. Free only holds for a standalone external read.
Go deeper:
Solidity docs: view functions — what counts as reading versus modifying state.
ethereum.org: gas and fees — why reads are free but state changes are not.