关于递归,不解!
程序代码:
#include <iostream> using namespace std; void countdown(int n); int main() { countdown(4); // 调用递归函数 return 0; } void countdown(int n) { cout << "Counting down ... " << n << endl; if (n > 0) countdown(n - 1); // 函数调用本身 cout << n << ": Kaboom!\n"; }
为什么会输出如下:
Counting down ... 4
Counting down ... 3
Counting down ... 2
Counting down ... 1
Counting down ... 0
0: Kaboom!
1: Kaboom!
2: Kaboom!
3: Kaboom!
4: Kaboom!
Counting down ... 3
Counting down ... 2
Counting down ... 1
Counting down ... 0
0: Kaboom!
1: Kaboom!
2: Kaboom!
3: Kaboom!
4: Kaboom!
有点懵,求解!