C Recursion

# C Recursion

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.

### Quiz on Recursion in C and C++

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12
13. 13
14. 14
15. 15
1.  Current
2.  Review / Skip
4.  Correct
5.  Incorrect

1. ##### 1. Question

What will be the output of the following program?

#include <iostream>

using namespace std;

int fact(int x)

{

if(x<=1)

return 1;

else

return (x*fact(x-1));

}

int main()

{

int x=5;

int f=fact(x);

cout<<f;

}

•  5
•  120
•  12
•  1

## 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.

C Tutorials

C Interview Questions

•
•

C Quiz

•
•
•

C++ Tutorials

•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•

C++ Interview Questions

•
•