Use of recursion for repetitive algorithms

In simple words, recursion is when a function calls itself, creating a loop. Recursion could be used to complete the fibbonacci serie, here is an example of a program with recursion:

 <iostream>

using namespace std;

int fib(int x) {
    if (x == 1) {
        return 1;
    } else {
        return fib(x-1)+fib(x-2);
    }
}

int main() {
    cout << fib(5) << endl;
}

As you can see in the example above, the function fib is used inside the same funtion
)it is calling itself).

Thank you for reading my explanation, I hope it have helped you. For a more complete
explanation take a look at this link:

http://computacion.cs.cinvestav.mx/~acaceres/courses/estDatosCPP/node37.html

CC BY 4.0 Use of recursion for repetitive algorithms by diego plascencia is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.