What is Recursion in C++ | Types of Recursion in C++ ( With Examples )

What is Recursion in C++ | Types of Recursion in C++ ( With Examples )

21 May 2024
Intermediate
1.54K Views
5 min read
Learn via Video Course & by Doing Hands-on Labs

C++ Programming For Beginners

Recursion in C++: An Overview

In the previous tutorials, we went through various aspects of functions in C++like function parameters, call by value and call by reference, etc. Hence, we have become familiar with functions and its uses in programming. So, in this C++ tutorial, we are now moving towards recursion, a little advanced but very important technique in C++ programming. You can even check out our C++ Certification program for more understanding.

What is Recursion in C++?

Recursion is the process in which a function calls itself again and again. It entails decomposing a challenging issue into more manageable issues and then solving each one again. There must be a terminating condition to stop such recursive calls.

What is Recursion in C++

Syntax

recursionfunction()
{ 
recursionfunction(); //calling self function 
} 

Example

 #include <iostream>
using namespace std;
int fibonacci(int);

int main() {
 int n, f;
 n=12;
 f = fibonacci(n);
 cout << f;
 return 0;
}

int fibonacci(int n) {
 if (n == 0) {
 return 0;
 } else if (n == 1) {
 return 1;
 } else {
 return fibonacci(n - 1) + fibonacci(n - 2);
 }
}
  • This C++ program in C++ Editor uses recursion to determine the nth Fibonacci number.
  • It accepts the input n and returns 0 or 1 depending on whether n is 0 or 1.
  • Otherwise, it adds the two Fibonacci numbers before calculating the Fibonacci number recursively.

Output

if n = 12 then output will be: 
144

Read More - C++ Interview Interview Questions and Answers

Advantages of Recursion

  • The code can be made shorter with the help of recursion.
  • It offers a clear and straightforward method for writing the code.
  • It reduces the need to call the function repeatedly.
  • In issues like tree traversals as well as theTower of Hanoi, recursion is recommended.

Disadvantages of Recursion

  • Slower than non-recursive functions.
  • The code is difficult to decipher or comprehend.
  • Debugging can be difficult as compared to iterative programs.
  • Its space requirements are more compared to iterative programs.
  • Due to function calls, it requires more time than usual.
Summary

Recursionis a very useful approach when it comes to programming. It has wide applications ranging from calculating the factorial of a number to sorting and traversal algorithms. If you want to learn such an important concept in more depth, just consider our C++Certification Program. It will prove a practical booster in your journey of programming.

Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
Angular Certification Course Jun 16 SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
ASP.NET Core Certification Training Jun 17 MON, WED, FRI
Filling Fast
07:00AM to 08:30AM (IST)
Get Details
.NET Microservices Certification Training Jun 23 SAT, SUN
Filling Fast
05:30PM to 07:30PM (IST)
Get Details
ASP.NET Core (Project) Jun 23 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
Azure Developer Certification Training Jun 23 SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
React JS Certification Training | Best React Training Course Jun 30 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details
ASP.NET Core Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Advanced Full-Stack .NET Developer Certification Training Jun 30 SAT, SUN
Filling Fast
10:00AM to 12:00PM (IST)
Get Details
Generative AI For Software Developers Jul 14 SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this