Return values in Blockchain / Solidity - Mini Project: Build & Apply
favoriteNumber of type uint to store the user's favorite number.Use uint public favoriteNumber; inside the contract to create the variable.
setNumber that takes a uint parameter named _number and sets favoriteNumber to this value.Define function setNumber(uint _number) public and assign favoriteNumber = _number;.
getNumber that returns a uint and returns the value of favoriteNumber.Use function getNumber() public view returns (uint) and return favoriteNumber.
setNumber(42), then call getNumber() and print the returned value.Use a script to call setNumber(42) then getNumber() and print the result.
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
