Recursive function c++ programing

Recursive function may seem like a never ending loop, or like a dog chasing its tail. It can never catch it. So too it seems our function will never finish. This might be true is some cases, but in practise we can check to see if a certain condition is true and in that case exit (return from) our function. The case in which we end our recursion is called a base case.
void area() { // function calls itself area(); } int main() { area(); }

Example :

#include<iostream>
using namespace std;
int factorial(int i)
{
    if(i <= 1)
    {
        // This is base case
        return 1;
    }

    return i * factorial(i - 1);
}

int  main()
{
    int i = 15;
    cout<<"Factorial of "<<i<<" is "<<factorial(i)<<endl;
    return 0;
}

Output
Factorial of 15 is 2004310016

0 Comments