MCS-201 · Unit 10

Data Structures and Control Statements in Python

Section 12 of 12

Block 3 · Unit 10Data Structures and Control Statements in Python8 min · Section 12 of 12

10.12Recursion

The programme guide includes recursion in this unit. A recursive function solves a problem by calling itself on a smaller version of the same problem. Functions are defined with def (Unit 11 covers functions in detail); here we only need def name(parameters): followed by an indented body, and return to send back a result. The printed unit does not include this material, so this section has no study-material section numbers.

The idea

Every recursive function needs two parts:

  1. A base case: the simplest input, answered directly (for factorial, 0! = 1).
  2. A recursive case: the answer expressed in terms of a smaller input (n! = n × (n − 1)!), so that the input moves towards the base case.

How it runs: the call stack

Each call gets its own frame on the call stack, holding its own parameters. Calls wait until the calls they made return.

factorial(3)
factorial(3) = 3 * factorial(2)          waiting
               factorial(2) = 2 * factorial(1)      waiting
                              factorial(1) = 1 * factorial(0)   waiting
                                             factorial(0) = 1   base case
                              factorial(1) = 1 * 1 = 1
               factorial(2) = 2 * 1 = 2
factorial(3) = 3 * 2 = 6

Recursion versus loops

RecursionLoop (iteration)
StyleMirrors problems defined in terms of themselves (trees, factorial, Towers of Hanoi)Step-by-step repetition
MemoryOne stack frame per active callConstant extra memory
LimitPython stops at about 1000 nested calls (RecursionError)No such limit
Speed in PythonSlower (function-call overhead)Faster

Python’s default limit can be read with sys.getrecursionlimit() (normally 1000). Python does not optimise tail calls, so very deep recursion should be rewritten as a loop.

Key takeaways

  • Recursion = a function calling itself on a smaller problem.
  • Base case stops the recursion; recursive case reduces the problem.
  • Each call occupies a stack frame; Python’s default limit is about 1000 levels.
  • Naive recursion can repeat work; memoisation or a loop fixes it.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

What is the purpose of the base case?

Show answer

Answer: B. To stop the recursion by answering the simplest input directly

Without a base case the calls never end and Python raises RecursionError.

Question 2 · multiple choice

What does this program print?

Python · quiz_rec.py
def f(n):
    if n == 0:
        return 0
    return n + f(n - 1)

print(f(4))
Show answer

Answer: C. 10

f(4) = 4 + 3 + 2 + 1 + 0 = 10. Multiplication would give 24.

Question 3 · multiple choice

What happens if a recursive function has no base case?

Show answer

Answer: D. Python raises RecursionError after about 1000 nested calls

Each call adds a frame to the call stack until Python’s recursion limit is reached.

Question 4 · multiple choice

What does this program print?

Python · quiz_rev.py
def rev(s):
    if s == "":
        return ""
    return rev(s[1:]) + s[0]

print(rev("MCA"))
Show answer

Answer: A. ACM

rev(“MCA”) = rev(“CA”) + “M” = (rev(“A”) + “C”) + “M” = “A” + “C” + “M”.

Question 5 · multiple choice

Which data structure does Python use to keep track of active function calls?

Show answer

Answer: C. A stack

The most recent call must finish first (LIFO), so call frames are kept on the call stack.

Question 6 · short answer

Write a recursive function to compute the sum of the first n natural numbers, and trace it for n = 3.

Show worked answer

def total(n): if n == 1: return 1 and otherwise return n + total(n - 1). Trace: total(3) = 3 + total(2); total(2) = 2 + total(1); total(1) = 1 (base case). Returning: total(2) = 2 + 1 = 3, total(3) = 3 + 3 = 6. The base case n = 1 stops the recursion, and each call reduces n by 1 so it always reaches the base case for n ≥ 1.

↑ ↓ move↵ openTab switch type