Recursive Subprog Sequence Control
Recursive Subprog Sequence Control
Recursive Subprograms
Recursion is the primary control mechanism for repeating sequences of
statements. Recursion in the form of recursive subprogram calls, is one of the
most important sequence-control structure in programming.
Recursive subprogram is one that calls itself (directly or indirectly) repeatedly
having two properties
It has a terminating condition or base criteria for which it doesn’t call itself
Every time it calls itself, it brings closer to the terminating condition
In Recursive subprogram calls A subprogram may call any other subprogram
including A itself, a subprogram B that calls A or so on.
The only difference between a recursive call and an ordinary call is that the
recursive call creates a second activation of the subprogram during the
lifetime of the first activation.
Both CIP and CEP are used to implement recursive subprogram
1
12/16/2020
Types of Recursion
Direct Recursion
A function is called a recursive function if it calls itself again and again .
void RecFun()
{
// Some code....
RecFun();
// Some code...
}
2
12/16/2020
Tail Recursion
A recursive function is tail recursive when recursive call is the last thing
executed by the function.
For example the following C++ function print() is tail recursive.
Example
void print(int n)
{
if (n < 0) return;
cout << " " << n;
3
12/16/2020