递归调用示例

编写一个递归函数加深对递归调用的理解:

//recur.cpp -- using recursion
#include <iostream>

void recur( int n);

void main()
{
	int n = 5;
	recur(5);
	std::cin.get();
}

void recur( int n)
{
	using namespace std;
	if( n > 0)
	{
		cout << n << " is in " << &n << " call "<< endl;
		recur( n-1 );
	}
		cout << n << " is in " << &n << " return" << endl;
}
运行效果如图:

递归调用示例_第1张图片

总结一下: 递归调用在语句执行顺序上和派生类的构造函数与析构函数的执行顺序非常相似。



你可能感兴趣的:(递归调用)