# Recursion in C/C++ – Simplify your Long Codes using Recursive Functions

Get Certified in C Programming and Take Your Skills to the Next Level

Recursion in C/C++ is nothing but a repetitive process to accomplish a particular task. Iteration and recursion somewhat serve the same purpose. After completing this tutorial, you will understand why we use recursion instead of iteration in some cases.

In this tutorial, we will discuss:

• Significance of Recursion
• Working of Recursion
• Example of Recursion
• Pros and cons of Recursion

Before we start the recursion tutorial, it is important to have a crystal clear understanding of functions in C Language.

## 1. What is Recursion?

Recursion refers to the processes of repetition of a particular task in order to achieve a specific purpose. Therefore, it is safe to say that a recursive function in C/C++ allows the programmer to call the same function within itself.Â In other words, a recursive function is simply a function that calls itself. It is terminated when the main condition no longer continues to be satisfied.

Employing if-else conditions to control recursion is a good programming practice. The basic idea behind recursion in C/C++ is to break the main problem at hand into smaller fragments that follow a logical sequence.

It is important to note that Iteration (Looping) and Recursion are totally two different concepts which cannot be confused at any cost.

## 2. Significance of Recursion Function in C/C++

One of the striking features of recursion is its ability to enhance code readability by keeping it short and sweet. However, it is a method you would not prefer to use if you want to reduce the run-time of your code. Iteration is generally a much faster process than compared to recursion.

There are a wide variety of problems that we can solve using recursion such as to compute the factorial of a number, to find the Fibonacci series in a given range, the various kinds of tree traversals and the famous Tower of Hanoi problem.

## 3. How Does Recursion in C/C++ Work?

The working of C/C++ recursive function is pretty simple. This is how it works:

```void recursive_function()
{
recursive_function(); // Here the function calls itself
}

int main()
{
recursive_function();
return 0;
}```

In order to understand how a recursive function really works, let us consider a standard problem where we can apply the concept of recursion.

Let us create a recursive function to compute the factorial of a number.

We know that factorial of a number n is given by 1 x 2 x 3 x â€¦â€¦ x n

#### Example of Recursion in C

Here is a program in C that computes the factorial of a number using recursion:

```#include <stdio.h>
int factorial(int number)
{
if (number > 0)
{
return number*factorial(number-1);
}
else
{
return 1;
}
}
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

int number;
printf("Enter a positive integer: ");
scanf("%d", &number);
printf("The factorial of the number %d is: %d\n", number, factorial(number));
return 0;
}```

Code on Screen-

Output-

#### Example of Recursion in C++

Here is a program in C++ that computes the factorial of a number using recursion:

```#include<iostream>
using namespace std;
int factorial(int number)
{
if (number > 0)
{
return number*factorial(number-1);
}
else
{
return 1;
}
}
int main()
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

int number;
cout<<"Enter a positive integer: ";
cin>>number;
cout<<"The factorial of the number: "<< number <<" is: "<< factorial(number)<< endl;
return 0;
}```

Code –

Output-

Elucidation:

• We create a function named factorial to compute the factorial of a positive integer using recursion with 1 argument with the return type integer.
• The number whose factorial is to be computed is given as a parameter to the function.
• If the number is equal to 0, the return value of the result would be 1. The only case would be number = 0 and we know that the factorial of 0 is 1.
• If the number is greater than 0, that is, the general case, we are supposed to multiply the number entered by the user with its preceding number, that is, number-1 till the number comes 1.

Suppose the user entered number = 5

Therefore, after each recursion, the value returned would be:

5 * factorial(4)
5 * 4 * factorial(3)
5 * 4 * 3 * factorial(2)
5 * 4 * 3 * 2 * factorial(1)
5 * 4 * 3 * 2 * 1 *factorial(0)

Here, when the number becomes 0, the condition terminates and we get the output as 120.

• Recursive functions in C enhance the readability of the program.
• Used to solve problems that are inherently recursive in nature such as tree traversal problems and the famous Tower of Hanoi problem.

• It requires a lot of storage space hence the program is comparatively slower than iterative functions.
• The time complexity of recursion is greater than iteration.

## Summary

In this tutorial, we discussed the meaning and purpose of recursive functions and recursion in C/C++.Â As of now, we can say that we are very well equipped with the skills of recursive functions in C that would help us out in the journey of C programming. We inferred that long lines of codes can be simplified by using recursion. Further, we carried on our discussion by understanding how recursion and recursive functions work. Finally, we concluded our discussion by stating the advantages and disadvantages of using recursion. Now, It is safe to say that we are much aware of Recursion in C/C++ and can use it effectively whenever and wherever required.

Get theÂ Samurai Technique to Larn Arrays in C

Don’t forget to give us your reviews in the comment section below.

You give me 15 seconds I promise you best tutorials