View and pure functions in Blockchain / Solidity - Time & Space Complexity
When working with blockchain smart contracts, view and pure functions let us read data without changing it.
We want to understand how the time to run these functions grows as the input size grows.
Analyze the time complexity of the following code snippet.
contract Example {
uint[] numbers;
function getSum() public view returns (uint) {
uint sum = 0;
for (uint i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
return sum;
}
function getConstant() public pure returns (uint) {
return 42;
}
}
This contract has a view function that sums all numbers in an array and a pure function that returns a fixed number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop in
getSum()that goes through each number in the array. - How many times: Once for each element in the
numbersarray. - Pure function:
getConstant()does not repeat any operation; it just returns a fixed value.
As the array size grows, the number of steps to sum all elements grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of items; double the items, double the work.
Time Complexity: O(n)
This means the time to run the view function grows linearly with the input size.
[X] Wrong: "View and pure functions always run instantly no matter the input size."
[OK] Correct: While pure functions with fixed work run quickly, view functions that loop over data take longer as data grows.
Understanding how reading data scales helps you write efficient smart contracts and answer questions about cost and speed.
"What if the getSum() function used recursion instead of a loop? How would the time complexity change?"
Practice
view function in a blockchain smart contract?Solution
Step 1: Understand the role of view functions
View functions are designed to read data from the blockchain without changing it.Step 2: Compare options with definition
It reads blockchain data but does not modify it. matches this definition exactly, while others describe modifying or ignoring blockchain data.Final Answer:
It reads blockchain data but does not modify it. -> Option AQuick Check:
View function = reads only [OK]
- Confusing view with functions that modify data
- Thinking view functions can write data
- Assuming view functions do not read blockchain data
Solution
Step 1: Recall Solidity function declaration syntax
Functions must specify visibility (e.g., public) and state mutability (pure/view) before returns.Step 2: Identify correct pure function syntax
function calculate() public pure returns (uint) {} correctly includes visibility and pure keyword before returns. function calculate() pure returns (uint) {} misses visibility, which is required.Final Answer:
function calculate() public pure returns (uint) {} -> Option AQuick Check:
Pure function syntax = public pure returns [OK]
- Omitting visibility keyword
- Using view instead of pure
- Placing pure after returns
function multiply(uint a, uint b) public pure returns (uint) {
return a * b;
}
// Called with multiply(3, 4)Solution
Step 1: Understand the function logic
The function multiplies two unsigned integers and returns the result.Step 2: Calculate the output for inputs 3 and 4
3 multiplied by 4 equals 12, so the function returns 12.Final Answer:
12 -> Option DQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Expecting state change error
- Confusing pure with view function behavior
view:uint count = 0;
function increment() public view returns (uint) {
count += 1;
return count;
}Solution
Step 1: Understand view function restrictions
View functions cannot modify state variables; they only read data.Step 2: Analyze the code behavior
The function increments a state variable, which is a write operation, causing an error.Final Answer:
Cannot modify state variable in a view function. -> Option BQuick Check:
View functions = no state changes [OK]
- Thinking view allows state changes
- Confusing pure and view keywords
- Ignoring state variable modification
Solution
Step 1: Identify function requirements
The function must not read or modify blockchain data, only use inputs to calculate sum.Step 2: Choose correct function type and declaration
Pure functions do not read or write blockchain data, so declaring it as public pure with inputs and returns is correct.Final Answer:
Use a pure function declared as function sum(uint a, uint b) public pure returns (uint). -> Option CQuick Check:
Pure = no blockchain data access [OK]
- Using view instead of pure
- Using payable unnecessarily
- Not specifying pure keyword
Ready to prove it?
Pass the test and this pattern moves unknown
