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
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-
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:
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.
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;
}
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.
Tags: advantages of recursion in cC Recursion FunctionC++ Recursionfactorial using recursion in crecursion in c examplesrecursion in c programmingRecursion in C TutorialWorking of Recursion in C
C Tutorials
C Interview Questions
C Quiz
C++ Tutorials
C++ Interview Questions
Home About us Contact us Terms and Conditions Privacy Policy Disclaimer Write For Us Success Stories
DataFlair © 2022. All Rights Reserved.