Bird
Raised Fist0
Blockchain / Solidityprogramming~5 mins

View and pure functions in Blockchain / Solidity - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is a view function in blockchain smart contracts?
A view function is a type of function that reads data from the blockchain but does not modify the state. It does not cost gas to call because it does not change anything.
Click to reveal answer
beginner
What does a pure function mean in blockchain smart contracts?
A pure function neither reads nor modifies the blockchain state. It only uses its input parameters to compute and return a result, without any side effects.
Click to reveal answer
intermediate
Why do view and pure functions not cost gas when called externally?
Because they do not change the blockchain state, the network does not need to record any changes, so calling them is free and does not consume gas.
Click to reveal answer
beginner
Can a view function modify the blockchain state?
No, a view function is not allowed to modify the blockchain state. It can only read data.
Click to reveal answer
intermediate
Give an example of when to use a pure function in a smart contract.
Use a pure function when you want to perform calculations or return a value based only on the input parameters, like adding two numbers, without reading or changing any stored data.
Click to reveal answer
Which of the following best describes a view function?
AOnly modifies blockchain data
BModifies blockchain data
CReads blockchain data but does not modify it
DNeither reads nor modifies blockchain data
What is true about pure functions in smart contracts?
AThey can read and modify blockchain state
BThey neither read nor modify blockchain state
CThey only read blockchain state
DThey only modify blockchain state
Why do view and pure functions not consume gas when called externally?
ABecause they do not modify the blockchain state
BBecause they modify the blockchain state
CBecause they are always free
DBecause they run on a different network
Which function type can perform calculations without accessing blockchain data?
AView function
BConstructor
CFallback function
DPure function
Can a view function call a pure function?
AYes, because pure functions do not modify state
BNo, because pure functions modify state
CNo, because view functions cannot call other functions
DYes, but only if the pure function modifies state
Explain the difference between view and pure functions in blockchain smart contracts.
Think about what each function can read or change on the blockchain.
You got /4 concepts.
    Describe why calling view or pure functions externally does not cost gas.
    Consider what causes gas fees in blockchain transactions.
    You got /4 concepts.

      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