Notes on View and Pure modifiers in Solidity

Simeon Campbell
CoinsBench
Published in
3 min readMay 2, 2024

--

A digital illustration showing a character with an Ethereum logo for a head, standing in an ancient library. The character is holding a calculator and giving a thumbs-up. The library is filled with rows of old books, lit by candles, conveying a blend of old-world knowledge and modern technology. This image symbolizes the integration of traditional learning with blockchain technology.

Why are there so many different modifiers for functions in the Ethereum EVM’s programming language Solidity? To answer this question, you must first understand what the Ethereum blockchain is.

What is the Ethereum Blockchain?

The Ethereum blockchain can be accurately described as a ‘decentralized immutable distributed ledger’. This may seem a little overwhelming, so let’s break it down.

  • Decentralized — There is no single centralized entity responsible for the ledger, but rather an entire network / community.
  • Immutable — It cannot be altered.
  • A distributed ledger — A shared accounting of the information and transactions.

So blockchains are just accounting systems that are openly shared by a wide number of people, where the information cannot be changed.

We see words like view , pure or payable appear after the function name. These are called modifiers, but what do they mean?

Solidity’s list of Modifiers from the official Solidity Docs cheetsheet. It can be found at — https://docs.soliditylang.org/en/v0.8.23/cheatsheet.html#modifiers
From the Solidity docs cheetsheet — https://docs.soliditylang.org/en/v0.8.23/cheatsheet.html#modifiers

There are many modifiers to choose from. For example:

  • payable allows for ETH to be received by a smart contract address.
  • immutable is used for state variables to be written and stored at construction time of the contract and stored on the blockchain forever.

The two that we are talking about today are the pure and view modifiers.

View Modifier

The view modifier declares that the function in question can only view and return blockchain data. It can’t write to or alter the state of the blockchain. One very common use of view functions is a get or a getter function.

function getUserBalance() public view returns (uint) {  
return users[msg.sender].userBalance;
}

For example, if you have a state variable that can change, using the contract, you may need access to the information. These are created automatically, with the public modifier on the variable itself. Or if the variable is private, you will have to write your own. Furthermore, if you declare a function as a view function, there is absolutely no way of spending gas when calling that function.

Pure Modifier

The pure function declares that you can’t write, alter, or even read the state of the blockchain. These functions are used for calculations carried out, that are not part of transactions that will be recorded on the blockchain.

function addition(uint a, uint b) public pure returns (uint) { 
return a + b;
}

One thing that they both have in common is that they are free to use, meaning they do not cost any gas. This is because they do not write any data to the blockchain. Of course, you could add some sort of calculation of math to a variable before it is returned, in a view function, if that is appropriate to your smart contract.

Conclusion

In conclusion, the view and pure modifiers are essential building blocks of the solidity (and indeed any blockchain) programming language. Due to the economic incentives of the gas feature, a smart contract wants to try to use as little gas as possible. Smart contract developers can utilize the view modifier to access public blockchain information, without having to write a transaction to do so.

In addition, they can use the pure modifier to carry out any mathematics off chain at no cost, before only using the return values in write functions, where necessary. The immutable nature of blockchains is certainly an excellent feature, which gives their trustless nature. The gas cost, however, is necessary, as it incentivizes people to write worthy information to the blockchains. pure and view functions give the programmer freedom over when and where to write to the blockchain. This allows for the most efficient smart contracts, whilst allowing blockchains to maintain their trustlessness.

References

Solidity Cheetsheet — Modifiers

My socials

Github — https://github.com/SimSimButDifferent

Twitter — https://twitter.com/Simsimbutdev

--

--