What is the difference between View & Pure Function Modifier in Solidity?

Yong kang Chia
1 min readMar 10, 2022

--

Function modifiers

You want to create a function without actually changing the state in Solidity — e.g. it doesn’t change any values or write anything.

What modifier could you use?

View

So in this case we could declare it as a view function, meaning it’s only viewing the data but not modifying it:

function sayHello() public view returns (string memory) {

Pure

Solidity also contains pure functions, which means you’re not even accessing any data in the app. Consider the following:

function _multiply(uint a, uint b) private pure returns (uint) {
return a * b;
}

This function doesn’t even read from the state of the app — its return value depends only on its function parameters. So in this case we would declare the function as pure.

Note: It may be hard to remember when to mark functions as pure/view. Luckily the Solidity compiler is good about issuing warnings to let you know when you should use one of these modifiers.

Adapted from CryptoZombies

--

--

Yong kang Chia
Yong kang Chia

Written by Yong kang Chia

Blockchain Developer. Chainlink Ex Spartan Group, Ex Netherminds

No responses yet