Practice Return Values in CS Thinking
Use these practice problems to test your method after reviewing the concept explanation and worked examples.
Quick Recap
The value that a function sends back to the code that called it, specified by the return statement.
A function is like a vending machine โ you put in inputs (arguments) and get back an output (return value). The return value is what comes out.
Example 1
mediumFUNCTION square(n): RETURN n * n. What is the output of: SET result = square(4) + square(3). OUTPUT result.
Example 2
mediumWhat is the difference between a function that returns a value and one that just prints? Compare: FUNCTION add(a,b): RETURN a+b vs FUNCTION printSum(a,b): OUTPUT a+b.
Example 3
mediumFUNCTION max(a, b): IF a > b THEN RETURN a ELSE RETURN b. What is max(max(3, 7), max(5, 2))?
Example 4
mediumWhat is the output of this program?
FUNCTION double(n)
RETURN n * 2
END FUNCTION
FUNCTION addThree(n)
RETURN n + 3
END FUNCTION
SET result = addThree(double(4))
OUTPUT result