So far, the recursive functions that are seen are directly calling themselves. Although most recursive functions conform to this form, the definition of recursion is more extensive, and if a function is subdivided into several child functions, a recursive call can be applied at a deeper nesting level. For example, if function f calls function g, and function g in turn calls function F, the invocation of these functions is still considered recursive. This type of
recursion is becoming an interactive recursive
The following shows the application of interactive recursion by determining whether a number is even or odd, and this topic highlights the importance of the trust of recursive jumps first of all, look at the odd and even-numbered descriptions:
If the first number of a number is odd, then the number is an even
A tree is either an even number or an odd number
definition 0 is an even
Trust of Recursive jumps
As you can see from the code, the implementation of the code is based entirely on the odd and even-numbered three points above. At first glance, this is so incredible. If you want to explore how the bottom is implemented, you can only use a small number of generation, trace call validation is OK
Simply from the surface, the simple scenario of "defining 0 is even" really doesn't see how this recursion works correctly. So, for the inability to see this situation, we need is recursive leap of trust, as long as we recursive decomposition of the correct and simple scenario analysis is correct, implementation details do not have to worry about, to the computer. And so, as long as we have a recursive mind, how simple and quick to solve a problem, how shocking
Copy Code code as follows:
#include <iostream>
using namespace std;
bool IsOdd (unsigned);
BOOL IsOdd (unsigned n)
{
Return! ( IsEven (n));
}
bool IsEven (unsigned n)
{
if (n = = 0)
{
return true;
}
Else
{
return isodd (n-1);
}
int main ()
{
cout << isodd (one) << Endl;
return 0;
}