Return Values CS Thinking Example 3

Follow the full solution, then compare it with the other examples linked below.

Example 3

medium
FUNCTION max(a, b): IF a > b THEN RETURN a ELSE RETURN b. What is max(max(3, 7), max(5, 2))?

Solution

  1. 1
    Step 1: max(3, 7) = 7. max(5, 2) = 5.
  2. 2
    Step 2: max(7, 5) = 7.

Answer

77
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