Return values let a function send back a result after it finishes. This helps you use that result later in your program.
Return values in Blockchain / Solidity
function functionName(parameters) returns (returnType) {
// code
return value;
}returns keyword specifies the type of value the function will send back.return inside the function to send the value back.function getBalance() public view returns (uint) {
return address(this).balance;
}function add(uint a, uint b) public pure returns (uint) {
return a + b;
}function getName() public pure returns (string memory) {
return "Blockchain";
}This contract has a function multiply that returns the product of two numbers. The testMultiply function calls multiply and returns a string showing the result.
pragma solidity ^0.8.0; contract SimpleCalculator { function multiply(uint x, uint y) public pure returns (uint) { return x * y; } function testMultiply() public pure returns (string memory) { uint result = multiply(3, 4); return string(abi.encodePacked("3 times 4 is ", uint2str(result))); } function uint2str(uint _i) internal pure returns (string memory str) { if (_i == 0) { return "0"; } uint j = _i; uint length; while (j != 0) { length++; j /= 10; } bytes memory bstr = new bytes(length); uint k = length; while (_i != 0) { k = k-1; uint8 temp = (48 + uint8(_i - _i / 10 * 10)); bstr[k] = bytes1(temp); _i /= 10; } str = string(bstr); } }
Functions can return only one value or multiple values using tuples.
Remember to match the return type in the function signature with the actual returned value.
In blockchain smart contracts, returning values helps verify results without changing the blockchain state.
Return values let functions send results back to where they were called.
Use returns to specify the type and return to send the value.
Return values help reuse results and check outcomes in blockchain code.
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
