Logo/
Johaan Mannanal

Shared by Johaan Mannanal

Log in

Make a lecture like this from your own PDF

Drop a PDF, slides or notes here. You'll sign up, then yours starts.

0:00 / 2:26

0:00 / 2:26The question
Java Recursion: Factorial(4) on the Call Stack
Johaan Mannanal

Shared by Johaan Mannanal

Summary

Recursion solves a problem by reducing it to a smaller version of itself. For factorial, the Java method returns 1 when n is 0; otherwise, it returns n multiplied by factorial(n - 1). For example, four factorial is 4 × 3 × 2 × 1 = 24.

When factorial(4) runs, it cannot complete its multiplication immediately, so it calls factorial(3), which calls factorial(2), then factorial(1), and finally factorial(0). Each call creates a stack frame containing postponed work. The base case, factorial(0), returns 1 and stops the chain.

The call stack then unwinds in reverse order. Factorial of 1 computes 1 × 1 = 1, factorial of 2 computes 2 × 1 = 2, factorial of 3 computes 3 × 2 = 6, and factorial of 4 computes 4 × 6 = 24. Thus, the stack grows while work is deferred and shrinks as the postponed multiplications are completed.

Full transcript

0:00Recursion can feel mysterious because a function seems to call itself forever. We will make it concrete with one small example: factorial of four. The goal is to connect three views of the same computation: the Java code, the growing call stack, and the final answer as the stack unwinds. The factorial of a nonnegative integer n, written n factorial, multiplies every positive integer from n down to one. So four factorial is four times three times two times one, which equals twenty-four. Recursion expresses that same pattern by reducing the problem from n to n minus one.

0:41Here is the Java method. If n is zero, it returns one immediately. Otherwise it returns n multiplied by factorial of n minus one. Notice the important pause: the multiplication cannot finish until the smaller recursive call returns. That unfinished multiplication is exactly what the call stack remembers. Start with factorial of four. It cannot compute four times anything yet, so it calls factorial of three and waits. Three calls two, and two calls one. Each new call becomes a frame above the previous frame. The stack is growing downward here, but conceptually it is a chain of postponed work.

1:25At the deepest point, factorial of one still needs factorial of zero. The base case is zero, and its answer is one. This case matters because it stops the chain. Without a base case, the method would keep creating calls until Java exhausted the stack and threw a StackOverflowError. Now the direction reverses. Factorial of zero returns one to the waiting call for one, so one times one gives one. That result returns to the frame for two, which computes two times one, giving two. Each frame resumes at the multiplication it paused before making its recursive call.

2:04Continue upward: three receives two and computes three times two, giving six. Then four receives six and computes four times six, giving twenty-four. The final value appears only after every postponed multiplication has been resolved. The stack first grows while work is deferred, then shrinks while answers flow back.

Ask AI about this video

Answers cite the source. Free account, no credit card.