Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

13 C++ Recursion (With Example)

Uploaded by

Muluken Dejen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

13 C++ Recursion (With Example)

Uploaded by

Muluken Dejen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Search tutorials and examples

C++ Recursion TOC

In this tutorial, we will learn about recursive function in C++ and its working with
the help of examples.

A DV E RT I S E M E N T S

A function that calls itself is known as a recursive function. And, this technique is
known as recursion.

Working of Recursion in C++

void recurse()
{
... .. ...
recurse();
... .. ...
}

int main()
{
... .. ...
recurse();
... .. ...
}

The figure below shows how recursion works by calling itself over and over again.

How recursion works in C++ programming

The recursion continues until some condition is met.

To prevent infinite recursion, if...else statement (or similar approach) can be used
where one branch makes the recursive call and the other doesn't.

Example 1: Factorial of a Number Using Recursion

// Factorial of n = 1*2*3*...*n

#include <iostream>
using namespace std;

int factorial(int);

int main() {
int n, result;

cout << "Enter a non-negative number: ";


cin >> n;

result = factorial(n);
cout << "Factorial of " << n << " = " << result;
return 0;
}

int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}

Output

Enter a non-negative number: 4


Factorial of 4 = 24

Working of Factorial Program

How this C++ recursion program works

A DV E RT I S E M E N T S

As we can see, the factorial() function is calling itself. However, during each
call, we have decreased the value of n by 1 . When n is less than 1 , the
factorial() function ultimately returns the output.

Advantages and Disadvantages of Recursion


Below are the pros and cons of using recursion in C++.

Advantages of C++ Recursion

It makes our code shorter and cleaner.

Recursion is required in problems concerning data structures and advanced


algorithms, such as Graph and Tree Traversal.

Disadvantages of C++ Recursion

It takes a lot of stack space compared to an iterative program.

It uses more processor time.

It can be more difficult to debug compared to an equivalent iterative


program.

Previous Tutorial: Next Tutorial:


C++ Storage Class C++ Return Reference

Share on: Was this article helpful?

A DV E RT I S E M E N T S

Related Tutorials

C++ Tutorial C++ Tutorial

C++ for Loop C++ if, if...else and Nested if...else

C++ Tutorial C++ Tutorial

C++ break Statement C++ Ternary Operator

Tutorials Examples
Join our newsle er for the latest Python 3 Tutorial Python Examples
updates.
JavaScript Tutorial JavaScript Examples

Enter Email Address* Join C Tutorial C Examples

Java Tutorial Java Examples

Kotlin Tutorial Kotlin Examples

C++ Tutorial C++ Examples

Swi Tutorial

C# Tutorial

DSA Tutorial

Company Apps

About Learn Python

Advertising Learn C Programming

Privacy Policy

Terms & Conditions

Contact

Blog

Youtube

© Parewa Labs Pvt. Ltd. All rights reserved.

You might also like