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. 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.

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.

Showing a random 20 of 50 problems.

Example 1

medium
`def f(a): result = a * 3` (no return). What does `r = f(2)` set `r` to?

Example 2

challenge
`def f(n): if n < 2: return n; return f(n-1) + f(n-2)`. What is `f(5)` (Fibonacci)?

Example 3

medium
`def f(): return` (bare return). What is the value of `f()`?

Example 4

medium
What 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 5

medium
`def f(n): return n % 2 == 0`. What does `f(6)` return and what does it mean?

Example 6

challenge
`def f(n): if n <= 1: return 1; return n * f(n - 1)`. What is `f(4)`?

Example 7

hard
`def f(n): if n == 0: return 0; return n + f(n-1)`. What is `f(5)`?

Example 8

medium
FUNCTION square(n): RETURN n * n. What is the output of: SET result = square(4) + square(3). OUTPUT result.

Example 9

easy
`def f(): return True`. Used in `if f():`, does the body run?

Example 10

medium
`def stats(xs): return min(xs), max(xs), sum(xs)`. What does `lo, hi, total = stats([3, 1, 4])` set the three variables to?

Example 11

medium
`def f(): x = 10; return x; x = 99`. What does `f()` return?

Example 12

easy
`def h(): print(7)`. What does `h()` return?

Example 13

medium
`def maxv(a, b): if a >= b: return a; return b`. What is `maxv(3, 8)`?

Example 14

medium
`def make_list(n): return [n, n*2, n*3]`. What is `make_list(4)`?

Example 15

medium
`def classify(n): if n < 0: return 'neg'; if n == 0: return 'zero'; return 'pos'`. What does `classify(0)` return?

Example 16

hard
Why is `return` preferred over `print` when a function's value is needed by other code? Compare `def add(a,b): print(a+b)` versus `def add(a,b): return a+b`.

Example 17

easy
A function `def f(): return 5`. What does `f()` evaluate to?

Example 18

medium
What 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

Example 19

medium
`def f(): pass`. What is `print(f())`?

Example 20

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