#100DaysOfSolidity 🔍 Understanding View and Pure Functions in Solidity 🔬 #18

#100DaysOfSolidity Series 018 “View & Pure Functions”

Solidity Academy
4 min readJul 3, 2023

In Solidity, getter functions can be declared as either “view” or “pure” to specify the behavior and restrictions of the function. These function modifiers ensure that the function adheres to certain constraints, such as not modifying the state or not reading or modifying any state variables.

#100DaysOfSolidity 🔍 Understanding View and Pure Functions in Solidity 🔬 #18

In this article, we will explore the concepts of view and pure functions in Solidity and understand their significance in smart contract development.

What are View Functions? 🧐

View functions in Solidity are designed to retrieve and return data from the blockchain without modifying the state of the contract. These functions are used to provide read-only access to the contract’s state variables. By declaring a function as “view,” you are promising that the function will not change the state of the contract or any variables it accesses.

Let’s take a look at an example to illustrate the usage of view functions:

contract ViewAndPure {
uint public x = 1;
// Promise not to modify the state.
function addToX(uint y) public view returns (uint) {
return x + y;
}
// … Other functions
}

In the above contract, the function `addToX` is declared as a view function. It takes a parameter `y` and returns the sum of `x` and `y`. Since the function is marked as “view,” it does not modify the state of the contract and can be safely called from other contracts or read by external applications.

When a view function is called, it does not require a transaction to be executed on the blockchain, making it a cost-effective way to retrieve information. View functions are useful when you need to fetch data from the contract without modifying anything.

What are Pure Functions? 🌟

Pure functions in Solidity take the concept of view functions a step further. Not only do they not modify the state, but they also do not read or access any state variables. Pure functions are entirely self-contained and rely only on the input parameters provided to them.

Let’s see an example to understand pure functions better:

contract ViewAndPure {
// … Other contract variables
// Promise not to modify or read from the state.
function add(uint i, uint j) public pure returns (uint) {
return i + j;
}
// … Other functions
}

In the above contract, the function `add` is declared as a pure function. It takes two parameters `i` and `j` and returns their sum. Since the function is marked as “pure,” it doesn’t read or modify any state variables. Pure functions are deterministic, meaning that given the same input, they will always produce the same output. They can be executed locally without interacting with the blockchain, making them more efficient than view functions.

Benefits of Using View and Pure Functions 💡

1️⃣ Security: By explicitly declaring functions as view or pure, you provide clarity to other developers and auditors regarding the intended behavior of the function. This helps prevent unintentional modifications to the state and minimizes the risk of introducing vulnerabilities.

2️⃣ Gas Efficiency: View and pure functions do not require transactions to be executed on the blockchain. As a result, they do not consume any gas, making them more cost-effective when retrieving data or performing computations.

3️⃣ Code Reusability: View and pure functions can be called by other functions within the same contract or by external contracts. This promotes code reusability and modular design, as these functions can be used in multiple contexts without concerns about state modifications.

4️⃣ Optimization: Since pure functions are entirely self-contained and do not rely on external variables, they can be optimized by the compiler more aggressively. This can lead to improved performance and reduced gas costs.

🔎 Detailed Analysis, Comparison, and Comments on the Smart Contracts: 📝

contract ViewAndPure {
uint public x = 1;
function addToX(uint y) public view returns (uint) {
return x + y;
}
function add(uint i, uint j) public pure returns (uint) {
return i + j;
}
}

In the above smart contract, we have two functions, `addToX` and `add`, with different function modifiers: `view` and `pure`, respectively.

The `addToX` function is marked as `view` since it does not modify the state of the contract. It takes a parameter `y` and returns the sum of `x` and `y`. This function is suitable for situations where you need to retrieve the result of the addition without altering any contract state.

On the other hand, the `add` function is marked as `pure` because it not only avoids modifying the state but also refrains from accessing any state variables. It takes two parameters `i` and `j` and returns their sum. This function is useful when you need to perform calculations that solely depend on the input parameters and do not require any interaction with the contract’s state.

By using `view` and `pure` function modifiers appropriately, you can clearly convey the intentions of your functions and ensure that they adhere to the expected behavior. It enhances the readability and maintainability of your codebase, making it easier for other developers to understand and work with your smart contracts.

📝 Conclusion:

In Solidity, `view` and `pure` function modifiers provide a way to express the behavior and constraints of functions in smart contracts. By using these modifiers, you can explicitly indicate whether a function is meant to be read-only (`view`) or entirely self-contained and independent (`pure`). This improves code clarity, promotes code reusability, and enhances gas efficiency. Understanding and correctly applying these function modifiers is essential for building secure and efficient smart contracts.

🔗 Additional Resources:

✨📚 Happy coding and exploring the world of Solidity! 🚀🌟

--

--

Solidity Academy

Your go-to resource for mastering Solidity programming. Learn smart contract development and blockchain integration in depth. https://heylink.me/solidity/