Data Structures Question
When writing a recursive function, what are the two cases for which you must write code?
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. In the iterative program, a group of statements or a single statement is executed repeatedly.
The stack data structure is used in recursion for storing the intermediate value when each function call is made but in the iterative program, the stack is not used.
For example:
int calculateFactorial(int n)
{
//base condition
if (n < = 1)
{
return 1;
}
else
{
return n * fact(n-1);
}
}
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.
Data Structures Question When writing a recursive function, what are the two cases for which you...