akash11

View and Pure Functions in Solidity

Two special function types in Solidity that help you save gas and make your code more predictable by declaring whether functions read or modify blockchain state.

Published on 01 November, 2025

Categories:

Solidity

Understanding State in Smart Contracts

Before we talk about view and pure functions, you need to understand what "state" means. State is any data stored permanently on the blockchain. This includes:
  • Storage variables (the variables you declare at the contract level)
  • Account balances
  • Other contract data
  • Block information
Reading or writing this state costs gas. Solidity gives you two keywords to be explicit about how your functions interact with state.

View Functions

A view function promises it will only read state, never modify it. Think of it like looking through a window - you can see what's inside but you can't touch anything.
contract Counter { uint256 public count = 0; // This is a view function - it reads 'count' but doesn't change it function getCount() public view returns (uint256) { return count; } // Another view function that does a calculation function getDoubleCount() public view returns (uint256) { return count * 2; } }
When you call a view function externally (from outside the blockchain), it doesn't cost gas because you're just reading data. However, if another function in your contract calls a view function during a transaction, that still costs gas as part of the overall transaction.

Pure Functions

A pure function is even more restricted. It doesn't read state and doesn't modify state. It only works with the parameters you give it, like a calculator that has no memory.
contract MathHelper { // Pure function - doesn't read or write any state function addNumbers(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } // Another pure function function multiply(uint256 x, uint256 y) public pure returns (uint256) { return x * y; } }
Pure functions are useful for utility functions that perform calculations or data transformations without needing any contract data.

What Counts as Modifying State

These actions will break the view/pure promise:
  • Writing to storage variables
  • Emitting events
  • Creating other contracts
  • Using selfdestruct
  • Sending Ether
  • Calling functions that aren't view or pure
  • Using low-level calls
  • Using inline assembly that writes to state

What Counts as Reading State

These actions will break the pure promise (but are fine for view):
  • Reading storage variables
  • Accessing address(this).balance or any address balance
  • Accessing block, tx, or msg properties (except msg.sig and msg.data)
  • Calling functions that aren't pure
  • Using inline assembly that reads from state

Practical Example

Here's a contract that shows all three types working together:
contract Example { uint256 public storedNumber = 42; // Regular function - modifies state function setNumber(uint256 newNumber) public { storedNumber = newNumber; } // View function - reads state but doesn't modify function getNumber() public view returns (uint256) { return storedNumber; } // View function - reads and does calculation function getNumberPlusTen() public view returns (uint256) { return storedNumber + 10; } // Pure function - no state involved at all function calculate(uint256 a, uint256 b) public pure returns (uint256) { return a + b; } }

Why This Matters

Using view and pure correctly gives you three benefits:
  1. Gas savings: External calls to view and pure functions don't cost gas when called directly
  2. Safety: The compiler enforces these promises, catching bugs where you accidentally modify state
  3. Clarity: Other developers know immediately what your function does just by seeing the keyword

When to Use Each

Use a regular function when you need to modify blockchain state. Use view when you need to read state but not change it. Use pure when you're doing calculations or transformations that don't need any contract data at all. The compiler will help guide you by throwing errors if you use the wrong one.

Copyright © 2025

Akash Vaghela