×
BLUE
C++ Function Recursion


C++ Function Recursion
  14 June 2021
Read more Like this Post.
In function recursion function call itself like a loop. Function calls itself when we not stop it. For stopping a function recursion we add some condition on it.
In the code a simple programme which have a function with name of _rec_ and pass in a argument type integer in function we made a condition with which false on if the integer x is equal to 5 otherwise if its false the function return itself again and make a increment in x value and pass again in function. This function calling from when the condition is be true. If the condition is false it returns the value of x This function recursion work like a looping.

Example: finding factorial in function recursion

#include 
using namespace std;

// section 1
int _rec_(int x){
    if(x > 1){
        return x * _rec_( x - 1 );
    }else{
        return 1;
    }
}

int main(){
    int z = 13;
    // section 2
    int output = _rec_(z);
    cout << output << endl;
}
//output: 1932053504 
In above code section 1 have a function which take a parameter ( x ) in the function have a condition which is true if x > 1 and return the function again with multiplication of x value and passing x again this time x is decremented by 1.

Make another program which show the febnochi series

#include 
using namespace std;

// section 1
int _fib_(int x){
    if((x == 1) || (x == 0))
        return x;
    else
        return (_fib_( x - 1 )   _fib_( x - 2 ));
}

int main(){
    int x, i = 0;
    cout << "Enter the number to find series: ";
    cin >> x;
    cout << "\n Series: ";
    // section 2
    while(i < x){
        cout << "  " << _fib_(i);
        i  ;
    }
}
//ouput is:   0  1  1  2  3  5  8  13  21  34  55  89  144  233  377  610  987  1597  2584  4181
In the code section 1 make a function name _fib_ its take 1 arguments which is x and a condition when x == 1 or x == 0 function return the x value. Otherwise its call the function again and again in 2nd section a while loop which print the series of numbers.

Direct recursion

If the function call itself this method call direct recursion mean a function call itself directly.

Example: of the direct recursion

#include 
using namespace std;

int _fun_(){    
    return _fun_();
}
int main(){
    _fun_();
}

This is the example of code in the code the function call itself directly for infinite time. This method called direct recursion function called.

Indirect recursion

When a function call another function or another function call this function it’s called indirect recursion. The example of indirect recursion:

Example:

#include 
using namespace std;
// section 1
int _fun_();
int _fun_x_();

// section 2
int _fun_(){    
    return _fun_x_();
}

int _fun_x_(){    
    return _fun_();
}

int main(){
// section 3
    _fun_();
}
In above example there is two function _fun_() and _fun_x_(). In section 1 we made function prototype’s in section 2 we call the first function _fun_() and return the second function in second function return the first. In section 3 first function were called only. First function calling in second function its called indirect recursion function called.

Indirect recursion function examples

#include 
using namespace std;
// section 1
int _fun_(int);
int _fun_x_(int);

// section 2
int main(){
    cout << _fun_(1);
}

// section 3
int _fun_(int x){
    cout << x << "\t";
    if(x == 20){
        return x;
    }else{
        return _fun_x_(x);
    } 
}

int _fun_x_(int x){
    // section 4
    return _fun_(  x);
}
// ouput is: 1       2       3       4       5       6       7       8       9       10      11        12      13      14      15      16      17      18      19      20 
This program will print the number 1 from 20. In section we created the prototype of the functions in section 2 call the function _fun_(1) with integer argument in section 3 the exact function which print the x value and check the condition if x == 20 is true its return x the final value if the condition is not true that will return and call the function _fun_x_( x ). In section 4 _fun_x_(x) this function calling from function 1 this function only return the function 1 and pass the parameter back with pre-increment.
Related Post's

cpp: functions   strings   classes   function overloading  
Not Loaded Zohaib Jozvi
0 14 June 2021
Read Latest Post.
Read other post.