function in C++

Recursion in Cpp programming language

Recursion in Cpp programming language

In this tutorial, we will discuss recursion in Cpp programming language

A function calling itself is called a recursive function. another word, a function calling from the definition of the same function is known the recursive function and this procedure is known as recursion

Example of recursion function

Recursive function in Cpp

How recursion works in C++

Recursive function

 

The flow of the recursive function

The flow of control of recursive in C++

The recursion continues until some condition fulfilled

Example

Find factorial of a number using a recursive function in C++

#include <iostream>
#include <conio.h>
using namespace std;
int find_Factorial(int num);
int main()
{
    int num;
    cout<<"Enter the number to find factorial: ";
    cin>>num;
    cout<<"Factorial of" << num<<": "<<find_Factorial(num);
    getch();
    return 0;
}
int find_Factorial(int num)
{
    if(num>1)
        return num*find_Factorial(num-1);
    else
        return 1;
}

When the above code is executed, it produces the following results

Enter the number to find factorial:4
factorial of 4: 24

In the above program, first, find_Factorial() function called inside the main () function with passing its argument.

when the value passed to the function, it is stored in the num variable.

Initially, the value of num is 4 to pass as an argument, then the function calls itself, in it the value of num reduce by 1 and it becomes 3 and it passes to beginning “find_Factorial()” function. it continuously reduces by one in every stage until it comes to 1.

when the num becomes less then 1, here the if condition returns false and flow of control moves to Else part for execution.

Else part is executed, excludes from the function

 

Explanation of recursive function to find factorial

Recursion in Cpp

 

 

 

Advantages of recursion in C++

Easy to understand and the code becomes readable and reduces the number of lines of the program.

This recursion  is used  to make a complex task easy and also  flexible and repeatedly functioning is easier with using nesting iteration

Disadvantages of recursion in C++

Tracing and debugging are very difficult

Every recursive lacks a separate memory location, as extra memory is required process becomes very slow

Similar post

Recursive function in Java

Recursive function in Python

Recursive function in C

Suggested for you

If statements in C++

The data type in C++

Variable in C++

Type of User defined function in C++

 

Recursion in C programming language
Calculate power of a number using recursion in C++
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

2 months ago