Recursion

Recursive programming is a procedure in which a method calls itself, so that a problem is solved more and more with each method call. This continues until the problem is reduced to a very simple case.

Some tasks in computer science can be solved well by reducing a large problem step by step to smaller and smaller problems of the same kind until simple (trivial) solutions arise . From this finally the solution of the original large problem is put together.

Syntax

return_type function_name(type parameter) { 
    // code to be executed 
    return function_name(type parameter)
}

Example: factorial

#include <iostream>
using namespace std;

int factorial(int number) {
    if (number < 2) {
        return 1;
    } else {
        return number * factorial(number - 1);
    }
}

int main() {
    cout << "factorial of 8 is " << factorial(8);

    return 0;
}
Output
factorial of 8 is 40320

Example: fibonacci

##include &lt;iostream&gt;
using namespace std;

int fibonacciAlgorithm(int number) {
    if (number == 0 || number == 1) {
        return number;
    } else {
        return fibonacciAlgorithm(number - 1) + fibonacciAlgorithm(number - 2);
    }
}

int main() {
    cout << "fibonacci number of the number 10 is " << fibonacciAlgorithm(10);

    return 0;
}
Output
fibonacci number of the number 10 is 55