The difference between ‘pure’ and ‘view’ functions in Solidity

Credit: Jon Tyson

In Solidity, pure and view are function modifiers. Modifiers are add-ons to functions that contain conditional logic [1].

A pure function:

function info() public pure returns (string memory) {
return 'This function is returning a basic string.';
}

A view function:

function hasValue() public view returns (bool) {
return value;
}

The difference between the two, is that a pure function cannot read/write/change state, it just executes [2]. The view function is actually looking up a state (true/false) and returning the value. Another example of a view function might be looking up a balance. Whereas another example of a pure function could be some sort of computation, like adding two parameters together.

Utilizing pure functions in some cases will consume no gas, therefore lowering the cost of operations in your smart contract applications [3].

[1] https://ethereum.stackexchange.com/questions/48971/what-are-function-modifiers

[2] https://ethereum.stackexchange.com/questions/28898/when-to-use-view-and-pure-in-place-of-constant#:~:text=As a rule of thumb,suggest the tightest restriction itself

[3] https://ethereum.stackexchange.com/questions/52885/view-pure-gas-usage-cost-gas-if-called-internally-by-another-function?noredirect=1&lq=1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store