In Solidity, understanding the difference between pure
and view
functions is crucial for writing efficient and effective smart contracts. These functions help in managing gas costs and ensuring the correct interaction with contract state variables. In this blog post, we will explore what pure
and view
functions are, their use cases, and how to implement them in your Solidity contracts.
Pure Functions:
Pure functions do not read or modify the state variables of the contract.
They can only use local variables and parameters passed to the function.
These functions do not interact with the blockchain, making them very efficient in terms of gas costs.
View Functions:
View functions read the state variables but do not modify them.
They provide a way to fetch data from the blockchain without changing the state.
These functions are also gas-efficient as they do not require a transaction to be mined.
Let’s look at an example to understand how to implement pure
and view
functions in Solidity:
// SPDX-License-Identifier: MIT
pragma solidity >=0.5.0 <0.9.0;
contract PureView {
string public Name = "lal";
uint public age = 24;
// View function to return the Name
function getName() public view returns(string memory) {
return Name;
}
// Pure function to perform a calculation and return the result
function calculate() public pure returns(uint) {
uint age1 = 24 + 1;
return age1;
}
}
State Variables:
string public Name = "lal";
anduint public age = 24;
are state variables stored on the blockchain.
View Function:
function getName() public view returns(string memory) {
return Name;
}
The
getName
function is a view function that reads the state variableName
and returns it.As it only reads and does not modify the state, it is marked with the
view
keyword.
Pure Function:
function calculate() public pure returns(uint) {
uint age1 = 24 + 1;
return age1;
}
The
calculate
function is a pure function that performs a simple arithmetic operation using local variables.It does not read or modify any state variables, hence it is marked with the
pure
keyword.Pure functions are the most gas-efficient as they do not interact with the blockchain.
View functions are also gas-efficient since they only read data without modifying the state.
Use
pure
functions for calculations and logic that do not require reading or writing state variables.Use
view
functions to fetch and return data from state variables without modifying them.Pure and view functions are declared with the
pure
andview
keywords respectively, ensuring clarity of their purpose and restrictions.
Understanding and effectively using pure
and view
functions in Solidity can greatly optimize your smart contract development by reducing gas costs and ensuring clear, maintainable code. Pure functions are ideal for logic and calculations that do not require interaction with state variables, while view functions are perfect for retrieving state data without making modifications.
References:
Written by ljb630 | Trying to find my place in the web3 community! | Technical Writer & Researcher | Team Lead