Return values in Blockchain / Solidity - Time & Space Complexity
When we look at return values in blockchain code, we want to know how the time to get that value changes as the input grows.
We ask: How long does it take to produce the return value when the input size changes?
Analyze the time complexity of the following code snippet.
function getBalance(address user) public view returns (uint) {
return balances[user];
}
This code returns the balance of a user from a stored mapping.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Direct lookup in a mapping (like a dictionary).
- How many times: Exactly once per call, no loops or repeated steps.
Getting the balance is a simple lookup that does not depend on how many users or balances exist.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays the same no matter how many users there are.
Time Complexity: O(1)
This means the time to get a return value is constant and does not grow with input size.
[X] Wrong: "Looking up a user's balance takes longer if there are more users."
[OK] Correct: The lookup uses a direct key access, so it takes the same time regardless of total users.
Understanding that return values from mappings are constant time helps you explain efficient data access in blockchain contracts clearly and confidently.
"What if the return value was computed by summing all balances instead of a direct lookup? How would the time complexity change?"
Practice
What does the return statement do in a blockchain smart contract function?
Solution
Step 1: Understand the purpose of return
Thereturnstatement is used inside a function to send a value back to whoever called the function.Step 2: Identify what return does in blockchain functions
In smart contracts, returning a value allows the caller to receive data from the function execution.Final Answer:
It sends a value back to the caller of the function. -> Option AQuick Check:
Return sends value back [OK]
- Thinking return creates transactions
- Confusing return with contract deployment
- Believing return changes contract address
Which of the following is the correct syntax to specify a function that returns an uint in Solidity?
function getValue() _____ { return 42; }Solution
Step 1: Recall Solidity function return syntax
In Solidity, the correct way to specify a return type isreturns (type)after the function parentheses.Step 2: Match the syntax with options
returns (uint) usesreturns (uint), which is the correct syntax.Final Answer:
returns (uint) -> Option DQuick Check:
Solidity uses returns (type) [OK]
- Omitting parentheses around type
- Using return instead of returns
- Placing return type inside function body
What will be the output of this Solidity function call?
function multiply(uint a, uint b) public pure returns (uint) { return a * b; }Calling multiply(3, 4) returns:
Solution
Step 1: Understand the function logic
The function multiplies two unsigned integersaandband returns the product.Step 2: Calculate the product of 3 and 4
3 multiplied by 4 equals 12, so the function returns 12.Final Answer:
12 -> Option BQuick Check:
3 * 4 = 12 [OK]
- Adding instead of multiplying
- Concatenating numbers as strings
- Expecting syntax error
Identify the error in this Solidity function that is supposed to return a bool:
function isEven(uint num) public pure returns (bool) { if (num % 2 == 0) { true; } else { false; } }Solution
Step 1: Check the return statements
The function usestrue;andfalse;withoutreturn, so it does not actually return any value.Step 2: Understand Solidity return requirements
Every return value must be preceded by thereturnkeyword to send the value back.Final Answer:
Missing return statements before true and false. -> Option AQuick Check:
Return keyword needed before values [OK]
- Omitting return keyword
- Confusing pure and view modifiers
- Thinking modulus operator is invalid
You want to write a Solidity function that returns the sum of all elements in a uint[] array. Which of the following implementations correctly returns the sum?
function sumArray(uint[] memory arr) public pure returns (uint) { uint total = 0; for (uint i = 0; i < arr.length; i++) { total += arr[i]; } _____ }Solution
Step 1: Understand the function logic
The function sums all elements in the array by adding each element tototal.Step 2: Identify the correct return value
After the loop, the sum is stored intotal, so returningtotalgives the correct sum.Final Answer:
return total; -> Option CQuick Check:
Return the sum variable total [OK]
- Returning array length instead of sum
- Returning loop index variable
- Returning the whole array instead of a value
Ready to prove it?
Pass the test and this pattern moves unknown
