C++Programming language

C++ Recursion

If a function is invoked in the exact same way within the function it’s called recursion. The function that calls the same function is referred to as a recursive function.

The function which calls itself but doesn’t do any work following the function call is referred to as tail theorem. In tail recursion, we usually use the same function by calling it with a return statement.

Let’s take a look at an easy illustration of the recursion.

recursionfunction(); //calling self function    

C++ Recursion Example

Let’s take a look at an example to print a factorial number by using the recursion feature in the C++ language.

using namespace std;    
int main()  
int factorial(int);  
int fact,value;  
cout<<"Enter any number: ";  
cout<<"Factorial of a number is: "<<fact<<endl;  
return 0;  
int factorial(int n)  
return(-1); /*Wrong value*/    
return(1);  /*Terminating condition*/  


Enter any number: 5
Factorial of a number is: 120

We can comprehend the above program of recursive method calls through the diagram below:

About author

A self-motivated, highly dedicated, and ambitious learner. Experienced Technical Content Writer with a demonstrated history of working in the e-learning industry.
Related posts
JAVAProgramming language

Java Convert CHAR to INT

JAVAProgramming language

Java Convert int to String

CProgramming language

C Dereference Pointer

JAVAProgramming language

Java Convert Strings to Character(char)