Have you ever heard of the term recursion? Do you want to know what is meant by “Recursion in C++“?
Today, we are going to talk about what is meant by recursion in C++. We will also see how to find the factorial of a number using recursion .
So, let’s get started!
What is recursion?
So, what is recursion in general?
If we type ‘recursion’ on our browser and hit search, it will give us the result that recursion is ‘the repeated application of a recursive procedure or definition.’
In computer science, this definition holds true as well. Would you like to know what is meant by recursion in C++?
If you’re looking for help with C++ homework then contact us!
What is Recursion in C++? Let’s get to know!
We use recursion in C++ mainly with functions. In simple words, when a function calls itself, the process is called recursion.
Moreover, the function that calls itself in its own body is said to be a recursive function.
In other words, recursion happens when a statement in the body of the function calls the function.
Furthermore, it should be noted that the function should have a terminating condition; also known as the base condition in its body.
If the terminating or the base condition is not present in the recursive function, the function will be invoked endlessly or enter an infinite loop.
This should be avoided. Hence, while working with recursion in C++, make sure to pay attention to the terminating condition.
Now, let us understand what is recursion in C++ with the help of an example.
Recursion in C++ – Example code.
We have an example program to help you understand what is recursion in C++ in a better way.
In the following example, to know what is meant by recursion in C++, we will see how to find the factorial of a number using recursion.
C++ programming language has many uses to know more you can check out our article on โWhat Are The Applications of C++ and Itโs Uses?โ
How to find the factorial of a number using recursion in C++?
Let us see how to find the factorial of a number using recursion with the code below.
Steps to find the factorial of a number using recursion:
- Input the number you want to find the factorial of.
- Call the function factorial() to find the factorial for the number the user has given as input.
- In the function factorial(), specify the base condition.
- The base condition in the function is the if statement. When the value of n will be equal to one, the recursive call will be terminated.
- To find the factorial of the number, recursively call the function factorial() using n*factorial(n-1).
- The function will then return the value of the factorial to the main function.
- Print the factorial.
C++ code to find the factorial of a number using recursion.
#include<iostream> using namespace std; // factorial() is a recursive function int factorial(int n){ if(n == 1) // base condition return 1; else return n*factorial(n-1); // calling the function recursively } int main(){ int n, Fact; cout<<"\t\t\t FACTORIAL USING RECURSION \n\n"; cout<<"Enter the number you want to find the factorial of: "; cin>>n; //input the number if(n < 1) cout<<"Please enter a natural number."; Fact = factorial(n); // calling the function cout<<"Factorial of "<<n<<" is "<<Fact; //printing the result return 0; }
Output
Suppose, the number you want to find the factorial of is 5.
The function factorial() calls itself to find the factorial of (n-1), which is 4 in this case, and returns 5*factorial(4).
The function again invokes itself and finds the factorial of (n-1) which now becomes 3 and returns 4*factorial(3) to the previously called function.
The process is repeated till the value of n becomes 1.
Thus, the result of factorial(1) is returned to factorial(2) which further returns the result to factorial(3) and so on.
Hence, when the function is terminated, we are able to find the factorial of 5 which is 120.
This is how recursion occurs in C++.
I hope now you know what is recursion in C++?
If you are curious to learn other topics on C++ then we have written some useful and detailed articles on the below topics:
- Learn about memory leaks in C++
- Know what is static function in C++
- Get to know about dijkastraโs algorithm in C++
- Have you wondered about the types of pointers in C++
- Learn how does bubble sort work in C++
- Know about selection sort in C++
We also offer a wide range of programming and coding help services for you to benefit from. Don’t miss out!