Consider the following Solidity function that returns a value. What will be the output when getValue() is called?
pragma solidity ^0.8.0; contract Test { function getValue() public pure returns (uint) { uint a = 5; uint b = 10; return a + b; } }
Look at the return statement carefully. It adds two numbers.
The function adds a and b which are 5 and 10 respectively, so it returns 15.
Given the following Solidity function, what is the output when getGreeting() is called?
pragma solidity ^0.8.0; contract Greeter { function getGreeting() public pure returns (string memory) { return "Hello, Blockchain!"; } }
Check the string returned exactly as written.
The function returns the string literal "Hello, Blockchain!" exactly as coded.
What will be the output of calling getData() in the following contract?
pragma solidity ^0.8.0; contract Data { function getData() public pure returns (uint, bool) { uint x = 42; bool y = true; return (x, y); } }
Look at the order of the returned values in the function signature.
The function returns a tuple with uint first and bool second, so the output is (42, true).
What error will occur when calling brokenFunction() in this contract?
pragma solidity ^0.8.0; contract Broken { function brokenFunction() public pure returns (uint) { uint a = 10; // Missing return statement } }
Functions with a return type must have a return statement.
The function declares it returns a uint but does not have a return statement, causing a compilation error.
Choose the statement that best describes how return values work in Solidity functions.
Think about Solidity's strict typing and return requirements.
In Solidity, if a function declares a return type, it must explicitly return a value of that type; otherwise, the compiler throws an error.