Written by **Sagar Rabidas**February 11, 2022

7 min read

C++

Software Developer

Recursion in c ++ manner growing a loop to carry out a procedure repetitively to finish a selected venture. Consequently, any feature that calls itself again and again in code is called a recursive characteristic. C ++ recursion is the most efficient and powerful manner of fixing large and complex mathematical problems by dividing them into smaller obligations small traces of code. The approach is also referred to as divide and overcome. The syntax for the Recursive function in C ++ is given below:

void recursive_function() // Name of the recursive function

{

// Here is the function the will calls itself

recursive_function() ;

}

int main ()

{

recursive_function() ;

return 0 ;

}

To understand how recursion works in C ++ let us see through a C ++ code that will help us understand step by step working of recursion in C++. But before that, we will discuss types of recursion in C ++ programming.

There are two types of recursion:

- Direct Recursion
- Indirect Recursion

When a function call itself directly, means it’s a direct recursive function. In the below syntax, you can see we have defined a function with the name recursive_function(). After that, we are calling the same recursive_function() inside recursive_fucntion(). This is the way of using direct recursion in your code.

void recursive_function()

{

recursive_function();

}

When a function calls itself indirectly, it means it’s calling the function with the help of another function defined is called indirect recursion. In the below syntax, you can see we have defined a function with name function () and inside that, we have defined a recursive_function(). After that, we are calling the function () inside recursive_fucntion(). This is the way of using indirect recursion in your code.

void function ()

{

recursive_function () ;

}

void recursive_function ()

{

function () ;

}

Below are the examples of Recursion in C++.

Here is the C + code to demonstrate the working of recursive function in the C ++ programming language:

#include<iostream>

using namespace std;

int main ()

{

int factorial_num ( int ) ;

int facto, value ;

cout << " Please enter any number to find factorial of it : " ;

cin >> value ;

facto = factorial_num ( value ) ;

cout << " The Factorial of given number is: " << facto << endl ;

return 0 ;

}

int factorial_num ( int n )

{

if ( n<0 )

return ( -1 ) ;

if ( n == 0 )

return ( 1 ) ;

else

{

return ( n*factorial_num (n-1) ) ;

}

}

Here is the C + + code to demonstrate the working of a recursive function and iterative function together in a single C + + programming language:

#include <iostream>

using namespace std ;

int factorial_fact ( int n )

{

if( n == 0 || n == 1 )

return 1 ;

return n * factorial_fact ( n-1 ) ;

}

int factorial_iterator ( int n )

{

int fact = 1 ;

for ( int i = n; i > 1; i--) {

fact *= i ;

}

return fact ;

}

int f[100] = {0} ;

int factorial_dynamic ( int n )

{

if (f[n] ) return f[n] ;

if ( n == 0 || n == 1 ) {

f[n] = 1 ;

return f[n] ;

}

f[n] = n*factorial_fact ( n-1 ) ;

return f[n] ;

}

int main()

{

cout << " The recursive factorial of number 15! = " << factorial_fact ( 15 ) << endl ;

cout << " The iterative factorial of the number 15! = " << factorial_iterator ( 15 ) << endl ;

}

C

C++

Recursion in C++

Recursion

Recursion function

Was this blog helpful?

Trending Technologies

15

Software91

DevOps48

Frontend Development24

Backend Development20

Server Administration17

Linux Administration28

Data Center24

Sentry24

Terraform23

Ansible83

Docker70

Penetration Testing16

Kubernetes21

NGINX20

JenkinsX17

Recommended Blogs

8

Recommended Threads

8

Anik Adhikary

Anik Adhikary

Anik Adhikary