📖 Scenario: You are creating a simple smart contract for a blockchain application. This contract stores a number and allows users to read it without changing the blockchain state. You will learn how to write view and pure functions, which do not modify the blockchain data.
🎯 Goal: Build a Solidity contract named NumberStore that stores a number and includes two functions: one view function to read the stored number, and one pure function to add two numbers without changing the contract state.
📋 What You'll Learn
Create a state variable called storedNumber of type uint.
Write a view function named getStoredNumber that returns the value of storedNumber.
Write a pure function named addNumbers that takes two uint parameters and returns their sum.
Deploy the contract and print the results of calling both functions.
💡 Why This Matters
🌍 Real World
View and pure functions are essential in blockchain smart contracts to read data efficiently and perform calculations without changing the blockchain state, saving gas fees.
💼 Career
Understanding these functions is crucial for blockchain developers to write optimized and secure smart contracts.
Progress0 / 4 steps
1
DATA SETUP: Create the contract and state variable
Create a Solidity contract named NumberStore and inside it declare a uint state variable called storedNumber initialized to 42.
Blockchain / Solidity
Hint
Use contract NumberStore {} to start and declare uint storedNumber = 42; inside.
2
CONFIGURATION: Add a view function to read the stored number
Inside the NumberStore contract, write a view function named getStoredNumber that returns the storedNumber variable.
Blockchain / Solidity
Hint
Use function getStoredNumber() public view returns (uint) and return storedNumber.
3
CORE LOGIC: Add a pure function to add two numbers
Inside the NumberStore contract, write a pure function named addNumbers that takes two uint parameters called a and b, and returns their sum.
Blockchain / Solidity
Hint
Define function addNumbers(uint a, uint b) public pure returns (uint) and return a + b.
4
OUTPUT: Print the results of calling the functions
Write a simple test in Solidity that calls getStoredNumber and addNumbers with 5 and 7, then returns their results. Use public functions to expose these calls.
Blockchain / Solidity
Hint
Create two public functions that call getStoredNumber() and addNumbers(5, 7) and return their results.
Practice
(1/5)
1. Which statement best describes a view function in a blockchain smart contract?
easy
A. It reads blockchain data but does not modify it.
B. It modifies blockchain data and stores new values.
C. It neither reads nor writes blockchain data.
D. It only accepts inputs and returns no output.
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 A
Quick Check:
View function = reads only [OK]
Hint: View functions read data but never change it [OK]
Common Mistakes:
Confusing view with functions that modify data
Thinking view functions can write data
Assuming view functions do not read blockchain data
2. Which of the following is the correct syntax to declare a pure function in Solidity?
easy
A. function calculate() public pure returns (uint) {}
B. function calculate() public view returns (uint) {}
C. function calculate() public returns (uint) {}
D. function calculate() pure returns (uint) {}
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 A
Quick Check:
Pure function syntax = public pure returns [OK]
Hint: Pure functions need visibility and 'pure' keyword before returns [OK]
Common Mistakes:
Omitting visibility keyword
Using view instead of pure
Placing pure after returns
3. What will be the output of this Solidity function call?
function multiply(uint a, uint b) public pure returns (uint) {
return a * b;
}
// Called with multiply(3, 4)
medium
A. Error: function cannot modify state
B. 7
C. 0
D. 12
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 D
Quick Check:
3 * 4 = 12 [OK]
Hint: Multiply inputs directly in pure function for output [OK]
Common Mistakes:
Adding instead of multiplying
Expecting state change error
Confusing pure with view function behavior
4. Identify the error in this Solidity function declared as view:
uint count = 0;
function increment() public view returns (uint) {
count += 1;
return count;
}
medium
A. Missing returns keyword.
B. Cannot modify state variable in a view function.
C. Function should be pure, not view.
D. No error, code is correct.
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 B
Quick Check:
View functions = no state changes [OK]
Hint: View functions cannot change state variables [OK]
Common Mistakes:
Thinking view allows state changes
Confusing pure and view keywords
Ignoring state variable modification
5. You want to create a function that calculates the sum of two numbers without reading or modifying any blockchain data. Which function type should you use and how should it be declared in Solidity?
hard
A. Use a view function declared as function sum(uint a, uint b) public view returns (uint).
B. Use a regular function declared as function sum(uint a, uint b) public returns (uint).
C. Use a pure function declared as function sum(uint a, uint b) public pure returns (uint).
D. Use a payable function declared as function sum(uint a, uint b) public payable returns (uint).
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 C
Quick Check:
Pure = no blockchain data access [OK]
Hint: Pure functions only use inputs, no blockchain data access [OK]
Common Mistakes:
Using view instead of pure
Using payable unnecessarily
Not specifying pure keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.