Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Recursion mechanics

โญ beginnerโณ 14 min read๐Ÿ“ Lesson 53 of 256

Base case, progress, the call stack, counting calls.

A recursive method calls itself on a smaller input. Two ingredients are mandatory:

  1. Base case โ€” an input answered directly, no call.
  2. Recursive case โ€” the call moves toward the base case (progress).
public static int factorial(int n) {
    if (n <= 1) {            // base case
        return 1;
    }
    return n * factorial(n - 1);   // progress: n shrinks by 1
}

The call stack makes traces mechanical. For factorial(4):

factorial(4) = 4 * factorial(3)
                 factorial(3) = 3 * factorial(2)
                                 factorial(2) = 2 * factorial(1)
                                                 factorial(1) = 1

Unwind: 1 โ†’ 2 โ†’ 6 โ†’ 24. Write the descent, hit the base, multiply back up.

Exam trace conventions: count calls (factorial(4) makes 4 total), and remember StackOverflowError is the runtime signature of missing progress โ€” an infinite loop's recursive twin.