In: Computer Science
a. In your own words, define the concept of "Recursion" in terms of C++ programming.
b. In your own words, define the concept of "infinite recursion".
c. In your own words, define "mutual recursion" as it pertains to C++ programming.
a.
Recursion:
The recursive function calls to itself and a terminate or base condition is specified which breaks the infinite execution of the recursive call. When the function call to itself then it works like a continue statement and the control goes to the starting of the function with updated argument value.
In a recursive program, the function call to itself and recursion is always applied to functions and the stack data structure is used in recursion for storing the intermediate value when each function call is made.
For example:
int fact(int n)
{
//base condition
if (n <= 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
b.
As we can see in the above recursive function, two cases are mention:
Base condition:
A terminate condition or base condition is specified which breaks the infinite execution of the recursive call.
Recursive Call:
The function will call to itself and this is known as a recursive call.
If the base condition is missing then infinite recursion will occur.
c.
Mutual Recursion:
Mutual recursion is recursion in which two recursive functions defined in terms of each other. The first function make a recursive call to the second function and the second function make a recursive call back to the first function.
For example:
int odd(int N)
{
if (N == 0)
return 0;
else
return even(N-1);
}
int even(int N)
{
if(N == 0)
return 1;
else
return odd(N-1);
}
The odd() function is calling even() function and even() function is calling odd() function.