递归函数,按定义说是函数自己调用自己,可要理解却很费劲,如下是从函数开辟栈空间的函数帧的角度,来阐释其原理。
#include <iostream>
using namespace std;
void fun(int n)
{
cout << "start fun,n: " << n << endl;
if(n == 0)
return;
int temp = n - 1;
fun(temp);
cout << "end of fun,n: " << n << endl;
}
int main()
{
int a = 5;
fun(a);
cout<< "after fun(a), main end..." << endl;
return 0;
}