Recursion and the Call Stack · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Functions that call themselves

Computing · Algorithms & Data Structures · ages 17-18
Name ______________________   Date ____________
  1. sum to n is n plus sum to n minus 1, with sum to 0 equal to 0. What does sum to 3 return?

    Answer: ______________

  2. If you write a recursive function but forget to give it a base case, it will keep calling itself until the program runs out of stack space and crashes.

    Circle one:   True   False

  3. A function is defined as sum_to(n) = n + sum_to(n - 1), with base case sum_to(0) = 0. What does sum_to(3) return?

    Answer: ______________

  4. Which line is the base case in count down, where n equals 0 returns done and otherwise it calls count down of n minus 1?

    • The line returning done when n equals 0
    • The line calling count down of n minus 1
    • The line defining count down
  5. Factorial of n is 1 when n is at most 1, else n times factorial of n minus 1. What is factorial of 4?

    Answer: ______________

  6. is_palindrome(s) compares s[0] to s[-1], then recurses on the middle: is_palindrome(s[1:-1]). The base case is when s has 0 or 1 letters. For the word "level", which call hits the base case?

    • is_palindrome("v")
    • is_palindrome("level")
    • is_palindrome("eve")
    • is_palindrome("ev")
  7. factorial(n) is defined as: if n <= 1, return 1; otherwise return n * factorial(n - 1). What is factorial(4)?

    Answer: ______________

  8. Which recursive function does the same job as this loop?

    total = 0 for i in range(1, n + 1): total += i return total

    • def total(n): if n == 0: return 0 return n + total(n - 1)
    • def total(n): return n + total(n)
    • def total(n): if n == 0: return n return total(n)
    • def total(n): return total(n - 1)
  9. power of x, n is 1 when n is 0, else x times power of x, n minus 1. From power of 2, 3, how many calls wait when power of 2, 0 is reached?

    • 1
    • 2
    • 4
  10. is palindrome compares outer letters then recurses on the middle, stopping at 0 or 1 letters. For level, which call hits the base?

    • is palindrome of eve
    • is palindrome of v
    • is palindrome of ev
LightMySky · lightmysky.comW1-mt_VGjfeF3F2e-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Functions that call themselves W1-mt_VGjfeF3F2e-s1

  1. 6 · It unwinds into 3 plus 2 plus 1 plus 0.
  2. True · Without a base case, the recursion never stops, so calls keep piling on the call stack until there is no room left.
  3. 6 · sum_to(3) unwinds into 3 + 2 + 1 + 0.
  4. The line returning done when n equals 0 · Only the base line returns a value without calling again.
  5. 24 · 4 times 3 times 2 times 1 is 24.
  6. is_palindrome("v") · "level" strips down to "eve", then to "v", which has just 1 letter and triggers the base case.
  7. 24 · factorial(4) = 4 x 3 x 2 x 1 = 24.
  8. def total(n): if n == 0: return 0 return n + total(n - 1) · The loop adds every number from 1 up to n. The matching recursive version needs a base case at 0 and must shrink n by 1 on every call.
  9. 4 · None has returned yet, so all four frames sit together.
  10. is palindrome of v · Level peels to eve and then to v, a single letter.
Worksheet · LightMySky