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].


[2] a rule of thumb,suggest the tightest restriction itself


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