Return Values CS Thinking Example 3
Follow the full solution, then compare it with the other examples linked below.
Example 3
mediumFUNCTION max(a, b): IF a > b THEN RETURN a ELSE RETURN b. What is max(max(3, 7), max(5, 2))?
Solution
- 1 Step 1: max(3, 7) = 7. max(5, 2) = 5.
- 2 Step 2: max(7, 5) = 7.
Answer
Return values enable function composition โ the output of inner function calls becomes the input of outer calls, allowing complex expressions to be built from simple functions.
About Return Values
The value that a function sends back to the code that called it, specified by the return statement. When a function executes a return statement, it immediately stops running and passes the specified value back to the caller, where it can be stored, used in expressions, or passed to other functions.
Learn more about Return Values โMore Return Values Examples
Example 1 medium
FUNCTION square(n): RETURN n * n. What is the output of: SET result = square(4) + square(3). OUTPUT
Example 2 mediumWhat is the difference between a function that returns a value and one that just prints? Compare: FU
Example 4 mediumWhat is the output of this program? FUNCTION double(n) RETURN n * 2 END FUNCTION FUNCTION addThr