递归函数如果调用了自己,那么被调用的函数也会调用自己。格式一般为
void 递归函数(参数1)
{
语句1;
if(判断)
递归函数(参数2);
语句2;
}
只要if判断是true,那么每次递归函数都调用语句1,然后调用递归函数(参数2),不调用语句2.
但当前的循环被结束时,即是if为false时,那么开始调用语句2,然后翻回上一个调用参数2,构成返回循环。如此两次调用构成对称参数。如下面程序对应。
#include<iostream>
void countdown(int n);int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << " Countdown down...." << n << endl;
if (n > 0)
countdown(n - 1);
cout << n << " : Kaboom!\n";
}
#include<iostream>
void countdown(int n);
int main()
{
countdown(6);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << " Countdown down...." << n << endl;
if (n > 0)
countdown(n -2);
cout << n << " : Kaboom!\n";
}
#include<iostream>
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << " Countdown down...." << n << endl;
/*if (n > 0)
countdown(n - 1);
cout << n << " : Kaboom!\n";
*/
}
#include<iostream>
void countdown(int n);
int main()
{
countdown(6);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << " Countdown down...." << n << endl;
if (n > 0)
countdown(n -2);
cout << n << " : Kaboom!\n";
}
#include<iostream>
void countdown(int n);
int main()
{
countdown(4);
return 0;
}
void countdown(int n)
{
using namespace std;
cout << " Countdown down...." << n << endl;
if (n > 0)
countdown(n - 1);
//cout << n << " : Kaboom!\n";
}