Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › RECURSION BASICS
For countdown(4), the current call only needs to handle 4. It prints 4, then asks countdown(3) to handle every number below 4. That call prints 3 and asks countdown(2) to continue, so the same decision repeats until the argument is 0.
void countdown(int n) {
if (n == 0) return;
cout << n << " ";
countdown(n - 1);
}
countdown(4);The arguments created by countdown(4) are 4, 3, 2, 1, and 0. The first four calls print their arguments, so the visible values are 4 3 2 1. The call with argument 0 stops without adding another value.
Which recursive call should countdown(n) make to handle all values below n?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The stopping check must happen before the print. When countdown(0) begins, if (n == 0) return ends that call immediately, so 0 never appears in the output. The call does not create another recursive call.
void countdown(int n) {
if (n == 0) return;
cout << n << " ";
countdown(n - 1);
}Without this check, countdown(0) would call countdown(-1), which would call countdown(-2), and the calls would continue because nothing makes the argument stop changing. In C++, that eventually exhausts the call stack instead of producing a finished countdown. Putting the check after the print would stop the further calls but would still print an unwanted 0.
Calling countdown(n - 1) does not replace the current call. The call for n = 4 remains paused after printing 4 while countdown(3) runs. The call for n = 3 is also paused while countdown(2) runs, and the same pattern continues through n = 1.
When countdown(4) starts, the active calls grow in this order: countdown(4), countdown(3), countdown(2), countdown(1), and countdown(0). The n = 0 call returns first. Then control returns through the paused calls for 1, 2, 3, and 4. Their paused work is already complete, so no additional numbers are printed.
Insert the base-case line so countdown(0) returns before the print.
void countdown(int n) {
cout << n << " ";
countdown(n - 1);
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
With the print before the recursive call, each number becomes visible while the calls are descending: 4, then 3, then 2, then 1. The smaller call still runs afterward, but it has no more printing to do when it returns.
// Print before the recursive call
void countdown(int n) {
if (n == 0) return;
cout << n << " ";
countdown(n - 1);
}
// Print after the recursive call
void countdownReverse(int n) {
if (n == 0) return;
countdownReverse(n - 1);
cout << n << " ";
}In the second version, countdown(4) first creates countdown(3), which creates countdown(2), which creates countdown(1), which creates countdown(0). Only after countdown(0) returns can the call for 1 print. Then 2, 3, and 4 print as their callers resume, producing 1 2 3 4. The calls were not replaced or finished when the smaller calls began, they were paused until those calls returned.