CS210 · Block 2 · Lesson 11

Recursion I: First Exposure

Concept · Base case · Recursive case · The call stack
At a glance: Your first exposure to recursion. CS110 did not cover it, so plan to read the supplementary notes carefully before class. The mental model you build today (base case, recursive case, the call stack) becomes the foundation for linked lists at Lesson 29 and the recursion-with-pointers work in Block 4. The recursion trace widget in Section 5 makes the call stack visible; the lab has you write your own.
Contents
  1. Lesson Objectives
  2. Assigned Readings
  3. Pre-Class Work
  4. Lesson Materials and Overview
  5. In-Class Work
  6. After Class
1. Lesson Objectives

By the end of this lesson, cadets will be able to:

  1. Identify the base case and the recursive case in a recursive function. (Outcome 1)
  2. Trace the execution of a recursive function and predict its return value. (Outcomes 1, 3)
  3. Describe how the call stack grows and shrinks during recursive execution. (Outcome 2)
  4. Write a small recursive function from a problem description. (Outcomes 1, 7)
  5. Diagnose a recursive function that is missing or mis-implementing its base case. (Outcome 3)
2. Assigned Readings

Complete before class. Recursion is new (CS110 did not cover it), so plan to read carefully.

Source Sections Why
Supplementary notes (required) Recursion I: First Exposure (all six sections) Vocabulary and the call stack model. About 25 minutes.
King, C Programming: A Modern Approach Section 9.6 (Recursive Functions), optional Optional deeper dive with additional worked examples

Note: Beej's Guide does not cover recursion as a topic. The supplementary notes are the assigned reading; they are short and focused.

3. Pre-Class Work
Do this first
Plan on 30 to 40 minutes for the reading. Most cadets find recursion confusing on the first read; the in-class trace widget makes it concrete. Come with specific questions.

Pre-Class Checklist

1
Read the supplementary notes
Section by section. Pay particular attention to:
  • The two pieces every recursive function has (Section 2)
  • The factorial(3) hand-trace (Section 3)
  • The call stack diagram with separate n values per frame (Section 4)
  • The two ways recursion gets it wrong (Section 5)
2
Print or download the reference card
A one-page printable summary of the reading. Keep it next to you during the in-class trace and the lab. Download the reference card (PDF) →
3
Try one trace on paper
Before class, predict by hand what factorial(5) returns and write down what the call stack looks like at its deepest point. The trace widget in class will confirm or correct your prediction.
4. Lesson Materials and Overview

Recursion is a way of solving a problem by breaking it into a smaller version of itself. The reference material below previews the concepts you saw in the supplementary notes; the in-class trace widget makes them visible in motion.

Jump to
Slides What is recursion Base case & recursive case The call stack Common pitfalls Decomposition connection DFCS standard

Slides

The Lesson 11 deck walks through the recursion concept, base case identification, the call stack model, factorial as the canonical example, and the two pitfalls. Roughly 30 to 40 minutes of lecture, then straight into the trace widget.

Open Lesson 11 slides →

What is recursion

A recursive function is a function that calls itself. That single sentence is the whole concept, but it lands strangely the first time you read it. The question that immediately follows is, "if a function calls itself, when does it stop?" The answer to that question is what makes recursion work.

Each call works on a smaller version of the problem. Eventually one call hits the simplest possible version, returns an answer directly, and the chain of waiting calls unwinds with values flowing back. The supplementary notes show this with a simple countdown example before moving to factorial.

Base case and recursive case

Every recursive function has exactly two pieces. Identifying them is the first thing you do when writing or reading recursive code.

Piece What it does
Base case The smallest version of the problem. Returns directly. No further recursive calls.
Recursive case Does a little work, then calls the function on a smaller input, then combines the result.

Factorial is the textbook example because the math itself is already recursive: n! = n · (n-1)! and 0! = 1. The math tells you what the base case and the recursive case should be.

int factorial(int n) {
  if (n <= 1) return 1;  // base case
  return n * factorial(n - 1);  // recursive case
}

The call stack

Every active function call has its own stack frame in memory: a little bundle holding that call's parameters, local variables, and a marker for where to return when the call finishes. The call stack is the collection of all currently-active frames, with the most recent call on top. A recursive call pushes a new frame; a return pops one.

During factorial(3), four frames are alive at once:

  +----------------------------+
  | factorial   n=1   ret=?    |  <-- top (most recent)
  +----------------------------+
  | factorial   n=2   ret=?    |
  +----------------------------+
  | factorial   n=3   ret=?    |
  +----------------------------+
  | main                       |  <-- bottom
  +----------------------------+

Each frame has its own copy of n. There are three separate n values alive at the same time, and they do not interfere with each other. This is the fact the trace widget in Section 5 makes visceral.

The two ways recursion goes wrong

Most broken recursion fails in one of two ways. Both involve the base case.

Pitfall 1: No base case at all.
The function calls itself forever. Stack grows without bound until the OS kills the program with a stack overflow.
Pitfall 2: Base case exists but is never reached.
The recursive call does not actually shrink the input toward the base case. Same crash, different cause. The lab includes one of these as a fix-it exercise.

Recursion as decomposition

In Lesson 6 you learned about functional decomposition: breaking a problem into smaller, well-named functions. Recursion is decomposition in its purest form. Every recursive function says, "this problem decomposes into a smaller version of itself, plus a tiny bit of glue." The base case and the recursive case are the two pieces that decomposition produces.

When you write the L11 lab functions, name the pieces explicitly to yourself before you write any code: what is the base case? and what is the smaller problem? If you can answer both in plain English, the C code follows quickly.

DFCS C Programming Standard

Recursive code is short but conceptually dense. A two-line recursive function can hide a base-case bug that takes an hour to find. The DFCS C Programming Standard §2.4 (Inline Comments) applies directly: a comment above the base case stating what the base case is, and a comment above the recursive call stating what gets smaller, makes the intent visible to a reader and to your future self when the program crashes.

The standard reminds you not to comment every line and not to rephrase code in English — comments explain code in the context of the problem. For L11, that means a one-line comment per function naming the base case and the recursive case, not blow-by-blow narration. See the Resources section for the full standard.

5. In-Class Work

Three things to do in class. Use the checklist to track your progress.

  • Step through the recursion trace widget. Five preloaded examples (countdown, factorial, sum_to_n, power, fibonacci). Watch frames push and pop. About 20 minutes.
  • Take the recursion self-check quiz. Eight quick questions on base cases, return values, stack depth, and the two pitfalls. About 10 minutes.
  • Complete Lab 11: Recursive Functions. Write three recursive functions and fix one that has a broken base case. Auto-graded through GitHub Classroom. Full instructions in the lab repo.

Recursion Trace

Five preloaded examples. Step through each one and watch the call stack grow and shrink. Mark all five complete to earn your code.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection.

CS210 Lesson 11 · Recursion Trace

Step through five recursive functions and watch the call stack grow and shrink. Each frame is a separate call with its own parameters. Mark all five examples complete to earn your code.
0 of 5 examples complete
Example 1 of 5
Current example
Loading...
Code
Call stack top = most recent
No frames yet.
Press Step or Run to start.
Status
Press Step to begin execution of this example.
How to read it: the highlighted line in the code pane is what is about to execute. The call stack pane shows every function call currently alive, with the most recent call on top. A new frame appears when a call is made; a frame disappears when its function returns, and the return value flashes briefly in the frame below. When the trace finishes, a short prediction question appears — answer it correctly to mark the example complete.

Recursion Self-Check Quiz

Eight questions covering base case identification, return-value tracing, stack depth, broken recursion, and the questions to ask before writing recursion. Answer each one, click Submit, then click Next. Score of 70% or higher earns the passing code.

Before you begin

Enter your name. It will appear on the completion screen so you can include it in your screenshot for the Blackboard reflection quiz.

CS210 Lesson 11 · Recursion Self-Check

Eight questions on base cases, recursive cases, return values, the call stack, and broken recursion. Answer each one, submit, and move forward.
How this works: One question per card. Pick or type your answer and click Submit. The feedback appears, then click Next to move on. Answers are locked once submitted, so think before you submit. When you finish all eight, you get a score and a recap.
6. After Class
  • Finish Lab 11 if you did not complete it in class. Submit through GitHub Classroom before Lesson 12.
  • Submit the Lesson 11 reflection in Blackboard: enter your widget codes and answer the short reflection prompt.
  • Read Beej-C Chapter 5 (Pointers I) for Lesson 12. Recursion is the last "first-exposure" topic before pointers; the next two weeks are the hardest stretch of the course.
  • Replay the recursion trace widget on your own if any example was confusing. Try predicting the return value and stack depth before pressing Step.
  • Heads up: Quiz 4 at Lesson 12 covers Recursion I.
Need help? Schedule EI with your instructor. Bring your specific recursive function, the input you traced, and where the prediction stopped matching reality. Recursion is one of those topics where one careful conversation closes the gap; vague worry tends not to.