Return Values Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Return Values.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

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

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Return values let functions communicate results back to their callers. A function without a return value just performs an action.

Common stuck point: Print and return are different. Print displays a value; return sends it back to the calling code for further use.

Sense of Study hint: When writing a function, decide what result the caller needs and return that value. When calling a function, capture the return value in a variable (result = myFunction()) or use it directly in an expression. Remember that print shows text on screen but does not return a value.

Worked Examples

Example 1

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

Answer

2525

First step

1
Step 1: square(4) returns 4 * 4 = 16.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan โ€” every worked solution, all subjects

Example 2

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 3

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 4

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

Example 5

hard
`def search(xs, t): for i, v in enumerate(xs): if v == t: return i; return -1`. What does `search([5,8,2,8], 8)` return, and why isn't the second 8 returned?

Example 6

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

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

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

Example 2

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 3

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

Example 4

easy
`def g(x): return x * 2`. What is `g(4)`?

Example 5

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

Example 6

easy
`def add(a, b): return a + b`. What is `add(2, 3)`?

Example 7

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

Example 8

easy
`def f(): return 1; return 2`. What does `f()` return?

Example 9

easy
`def sq(n): return n * n`. What is `sq(5)`?

Example 10

easy
`r = add(1, 1)` where `def add(a,b): return a+b`. What is `r`?

Example 11

medium
`def f(x): if x > 0: return "pos"; return "nonpos"`. What is `f(-3)`?

Example 12

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

Example 13

medium
`def double(n): return n + n`. What is `double(double(2))`?

Example 14

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

Example 15

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

Example 16

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

Example 17

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

Example 18

medium
`def f(a, b): return a, b + 1`. What does `x, y = f(3, 7)` set `x` and `y` to?

Example 19

medium
`def f(n): if n > 0: return 1; elif n == 0: return 0; else: return -1`. What is `f(0)`?

Example 20

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

Example 21

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

Example 22

challenge
`def f(x): for i in range(3): if i == x: return i` with no final return. What is `f(9)`?

Example 23

easy
`def f(): return 42`. What is the value of `f()`?

Example 24

easy
`def triple(n): return 3 * n`. What is `triple(7)`?

Example 25

easy
`def f(): print(1); return 2; print(3)`. What is the call's output and returned value?

Example 26

easy
`def add(a,b): return a+b`. What is `add(add(1,2), add(3,4))`?

Example 27

easy
`def f(x): return x > 0`. What is `f(-2)`?

Example 28

easy
`def greet(name): return 'Hi, ' + name`. What is `greet('Ada')`?

Example 29

medium
`def f(n): if n > 0: return 'pos'`. What does `f(-5)` return?

Example 30

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

Example 31

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

Example 32

medium
`def f(n): result = n * n`. Why does the caller see None even though the math is computed?

Example 33

medium
`def f(a,b): return a / b if b != 0 else None`. What is `f(10, 0)`?

Example 34

medium
`def double(n): return n*2`. What is `double(double(double(1)))`?

Example 35

medium
`def f(): for i in range(5): if i == 3: return i; return -1`. What does `f()` return?

Example 36

medium
`def abs_diff(a,b): return a - b if a >= b else b - a`. What is `abs_diff(3, 9)`?

Example 37

medium
`def make_adder(k): def add(x): return x + k; return add`. What is `make_adder(10)(5)`?

Example 38

hard
`def f(xs): for x in xs: if x < 0: return False`. What does `f([1, 2, -3, 4])` return, and what does it return for `[1, 2, 3]`?

Example 39

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

Example 40

hard
`def safe_div(a, b): if b == 0: return None; return a / b`. The caller uses `r = safe_div(8, 0); print(r * 2)`. What happens?

Example 41

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

Background Knowledge

These ideas may be useful before you work through the harder examples.

function programming