Bird
Raised Fist0
Blockchain / Solidityprogramming~10 mins

View and pure functions in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - View and pure functions
Call function
↓
Is function view or pure?
No→Modify state allowed
Yes↓
Read-only or no state access
↓
Return data without state change
↓
End function
This flow shows how view and pure functions are called and how they only read or do not access state, returning data without changing blockchain state.
Execution Sample
Blockchain / Solidity
contract Example {
  uint x = 10;
  function getX() public view returns (uint) {
    return x;
  }
  function add(uint a, uint b) public pure returns (uint) {
    return a + b;
  }
}
This contract has a view function that reads state and a pure function that only uses inputs to return a result.
Execution Table
StepFunction CalledState AccessActionReturn Value
1getX()Reads state variable xReturns x value10
2add(3, 4)No state accessCalculates sum 3+47
3getX()Reads state variable xReturns x value10
4add(10, 20)No state accessCalculates sum 10+2030
5End-No more calls-
💡 Execution stops after all function calls return their results without modifying state.
Variable Tracker
VariableStartAfter getX()After add(3,4)After getX() againAfter add(10,20)Final
x101010101010
a--3-10-
b--4-20-
Key Moments - 2 Insights
Why does the view function getX() not change the state variable x?
Because view functions are read-only and cannot modify state, as shown in execution_table rows 1 and 3 where x remains 10.
Why can the pure function add() not access the state variable x?
Pure functions do not read or modify state; they only use input parameters, as seen in execution_table rows 2 and 4 where add() uses only a and b.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the return value of getX() at step 3?
A7
B10
C30
D0
💡 Hint
Check the 'Return Value' column at step 3 in execution_table.
At which step does the function add() calculate the sum of 10 and 20?
AStep 4
BStep 1
CStep 2
DStep 5
💡 Hint
Look at the 'Function Called' and 'Action' columns in execution_table.
If the function getX() tried to modify x, what would happen according to the concept flow?
AIt would succeed and change x
BIt would ignore the change silently
CIt would cause an error because view functions cannot modify state
DIt would convert to a pure function
💡 Hint
Refer to the decision step 'Is function view or pure?' in concept_flow.
Concept Snapshot
View and pure functions in blockchain:
- View functions read state but do not modify it.
- Pure functions neither read nor modify state.
- Both return data without changing blockchain state.
- Use view for reading variables.
- Use pure for calculations using only inputs.
Full Transcript
This visual execution shows how view and pure functions work in blockchain smart contracts. View functions can read state variables but cannot change them, while pure functions cannot read or change state and only use their input parameters. The example contract has a state variable x set to 10. The getX() function is view and returns x's value without changing it. The add() function is pure and returns the sum of two inputs without accessing state. The execution table traces calls to these functions, showing their actions and return values. Variables like x remain unchanged throughout. Key moments clarify why view functions cannot modify state and why pure functions cannot access state. The quiz tests understanding of return values, function steps, and rules about state modification. The snapshot summarizes the key rules for using view and pure functions in blockchain programming.

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

  1. Step 1: Understand the role of view functions

    View functions are designed to read data from the blockchain without changing it.
  2. 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.
  3. Final Answer:

    It reads blockchain data but does not modify it. -> Option A
  4. 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

  1. Step 1: Recall Solidity function declaration syntax

    Functions must specify visibility (e.g., public) and state mutability (pure/view) before returns.
  2. 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.
  3. Final Answer:

    function calculate() public pure returns (uint) {} -> Option A
  4. 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

  1. Step 1: Understand the function logic

    The function multiplies two unsigned integers and returns the result.
  2. Step 2: Calculate the output for inputs 3 and 4

    3 multiplied by 4 equals 12, so the function returns 12.
  3. Final Answer:

    12 -> Option D
  4. 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

  1. Step 1: Understand view function restrictions

    View functions cannot modify state variables; they only read data.
  2. Step 2: Analyze the code behavior

    The function increments a state variable, which is a write operation, causing an error.
  3. Final Answer:

    Cannot modify state variable in a view function. -> Option B
  4. 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

  1. Step 1: Identify function requirements

    The function must not read or modify blockchain data, only use inputs to calculate sum.
  2. 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.
  3. Final Answer:

    Use a pure function declared as function sum(uint a, uint b) public pure returns (uint). -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard