What is the difference between `view` and `pure` functions in Solidity?
Okay, no problem. Let's talk about view
and pure
in Solidity. These two concepts are actually quite easy to distinguish.
The Difference Between view
and pure
: One Can Only Look, The Other Doesn't Even Bother to Look
Imagine you walk into a restaurant:
- A
view
function is like looking at the menu: You can check all the dishes, prices, and descriptions on the menu (reading the contract's state), but you cannot go into the kitchen and change the menu yourself (modifying the contract's state). After you've looked at the menu, the restaurant itself remains unchanged. - A
pure
function is like using your own calculator: You use it to calculate, for example,2 + 3 = 5
. This calculation process does not rely on the restaurant's menu at all (it doesn't read the contract's state), and certainly doesn't change the menu (it doesn't modify the contract's state). It only depends on the numbers you input into it.
Let's break this down in detail.
view
Functions (Read-Only Functions)
You can think of it as a "read-only" or "observer" function.
- Promise: I guarantee that I will not modify any data within the contract.
- What it can do: It can read the contract's state variables. For example, if a contract records who the
owner
is, or what thetotalSupply
of an ERC20 token is, aview
function can read and tell you this information. - What it cannot do: It cannot modify any state variables (e.g.,
owner = newAddress
is not allowed), cannot emit events, and cannot call other functions that would modify the state.
Simple Code Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 public myNumber = 42; // This is a state variable
// This function reads the value of the state variable myNumber
// It only "looks" and doesn't "modify", so it's view
function getMyNumber() public view returns (uint256) {
return myNumber;
}
}
In this example, getMyNumber()
merely reads the value of myNumber
and returns it. It doesn't change anything, so we mark it as view
.
pure
Functions (Pure Functions)
This one is even stricter; you can call it a "pure computation function."
- Promise: I not only don't modify contract data, I don't even bother to look at it.
- What it can do: It can only use the parameters passed to it during the function call, or local variables defined within the function, to perform calculations and return a result. It is completely isolated from the contract's storage state.
- What it cannot do: It cannot do anything that
view
functions cannot do, and it also cannot read the contract's state variables.
Simple Code Example:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleCalculator {
// This function only depends on the input a and b
// It has no relation to the contract's own state, so it's pure
function add(uint256 a, uint256 b) public pure returns (uint256) {
return a + b;
}
}
As you can see, this add
function acts like an independent calculator. You give it a
and b
, and it returns their sum. It completely disregards any data stored in the contract.
In Summary, an Overview
Feature | view Function | pure Function |
---|---|---|
Reads contract state variables | ✅ Can do | ❌ Cannot do |
Modifies contract state variables | ❌ Cannot do | ❌ Cannot do |
Relies on input parameters for computation | ✅ Can do | ✅ Can do |
Analogy | Like a librarian, only able to consult resources | Like a calculator, only processes numbers you give it |
Here's the key: Why distinguish them?
Distinguishing between view
and pure
is not just about making code clearer; more importantly, it's to save Gas!
-
External Calls are Free: When you call a
view
orpure
function from outside the contract (e.g., from your website frontend via web3.js), this operation is completely free and consumes no Gas. This is because it only reads data and does not create a new transaction on the blockchain. This is extremely useful for displaying on-chain data to users. -
Internal Calls Save Gas: When a contract function A calls another
view
orpure
function B, although it will consume some Gas, it is usually much cheaper than calling a function that modifies state, because they do not perform expensive storage write operations.
Therefore, as a competent smart contract developer, cultivating the good habit of correctly using view
and pure
is very important. If your function logic does not need to modify or read the state, you must mark it as pure
or view
. This not only makes the code safer and its intent clearer but also saves real money for users.