When a Function can call itself again and Again or When a Function call itself until a Condition is not to be False. When a function definition includes a call to itself, it is referred to as a recursive function and the process is known as recursion or circular definition. In this a function call itself repeatedly. In the Recursion we just have to make a One Time call and the will automatically call itself Again and Again.
When a recursive function is called for the first time, a space is set aside in the memory to execute this call and the function body is executed. Then a second call to a function is made; again a space is set for this call, and so on. In other words, memory spaces for each function call are arranged in a stack. Each time a function is called; its memory area is placed on the top of the stack and is removed when the execution of the call is completed.
To understand the concept of recursive function, consider this example.
Example : A program to demonstrate the concept of recursive function
using namespace std;
void reverse ();
int main ()
reverse () ;
cout<<” is the reverse of the entered characters”;
void reverse ()
cout<<”Enter a character ('/' to end program): “;
if (ch != '/')
The output of the program is
Enter a character ('/' to end program) : h
Enter a character ('/' to end program) : i
Enter a character ('/' to end program) : /
ih is the reverse of the entered characters
In this example, function reverse ()is called to accept a character from the user. The function reverse ()calls itself again and again until the user enters' /' and prints the reverse of the characters entered.
Note that the recursive functions can also be defined iteratively using "for", "while" and "do ...while" loops. This is because recursion makes the program execution slower due to its extra stack manipulation and more memory utilization. In addition, recursion sometimes results in stack overflow, as for each function call, new memory space is allocated to local variables and function parameters on the stack. However, in some cases, recursive functions are preferred over their iterative counterparts as they make code simpler and easier to understand. For example, it is easier to implement Quick sort algorithm using recursion.