View and Pure
View and Pure
Table of Content
S. No Topic
1 Introduction to View and Pure
2 Role and Functionality of View and Pure Function
3 Syntax and Implementation of View and Pure
4 Code Explanation
Page 1 of 3
View Function:
• Usage: Functions declared as view do not modify the state of the contract. They
promise read-only access to the contract's data.
• Purpose: Ideal for retrieving data from the contract without making any changes to the
state.
Syntax:
function functionName() view returns (datatype) {
// Function logic
}
pure Function:
• Usage: Functions declared as pure ensure that they neither read nor modify the
contract's state. They don’t even access external data.
• Purpose: Perfect for utility functions or mathematical calculations that do not require
contract state or external data.
Syntax:
function functionName() pure returns (datatype) {
// Function logic
}
Code Explanation:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ViewPureDemo {
uint256 public storedValue = 5;
Page 2 of 3
Multiply () Function (Pure):
• The multiply () function, declared as pure, performs a multiplication operation on two
numbers (a and b) without relying on or altering the contract state. It strictly deals with
pure computation.
In a bank, view functions act like a customer checking their account balance without making
any changes, while pure functions are like mathematical calculations performed without any
reference to the bank's account details.
These functions in Solidity demonstrate the use of view and pure modifiers, ensuring functions
are clearly marked based on their interaction with the contract's state or external data.
View:
• Reading Data: Functions marked as view do not modify the contract's state. They
only read data from the blockchain.
• Usage: Use view functions when you need to fetch data from the blockchain but don't
intend to modify any state variables.
Pure:
• No State Changes: Functions marked as pure don't read from or modify the
contract's state. They solely perform computations.
• Usage: Use pure functions for computations or transformations that do not involve
blockchain data access or state changes.
Usage in Smart Contracts:
• View: Use view functions for retrieving data from the blockchain without altering the
contract's state, like getting account balances or querying contract information.
• Pure: Use pure functions for calculations or transformations that don't require
blockchain interaction, such as mathematical operations or data formatting.
Real-Life Comparison:
Consider view as looking at a book without writing anything – you're reading the information
but not altering the book's content. Pure is like using a calculator to perform calculations – it
doesn't affect the information around you, only the computations you perform.
In Solidity, using view and pure appropriately helps clarify the function's purpose and
ensures no unintended changes to the contract's state, optimizing gas usage and contract
clarity.
Page 3 of 3