Recursion in C++

Recursivity of c

Recursion in C++

Hello ladies and gentle men today in this article I will be explaining a bout what is recursion, uses of it and some care that has to be taking when using it in your programme, this is only for c++ programmers thought the syntax is same as many of the programing language like JavaScript and so on. So, what are we waiting for let's do it.

What is Recursion?

Recursion can be defined as a process of making a function to called itself, that it's in a simple language but to know its actual meaning is:

Recursively is the property that functions have to be called by themselves. It is useful for many tasks, like sorting or calculate the factorial of numbers. For example, to obtain the factorial of a number (n!) the mathematical formula would be: n! = n (n-1) (n-2) (n-3) ................ 1

more concretely, 5! (factorial of 5) would be:

5! = 5 4 3 2 1 = 120

and a recursive function to calculate this in C++ could be:

image.png

Notice how in function factorial we included a call to itself, but only if the argument passed was greater than 1, since otherwise the function would perform an infinite recursive loop in which once it arrived to 0it would continue multiplying by all the negative numbers (probably provoking a stack overflow error on runtime).

This function has a limitation because of the data type we used in its design (long) for more simplicity. The results given will not be valid for values much greater than 10! or 15!, depending on the system you compile it.

care has to be taking:

=> Recursion is like loop you have to find a way to stop it after it finish its work.

=> And if not necessary is better to used normal loop that using recursion.

That it's, I hope you find something relevant in this small article and don't forget to like and do comment what ever you guys don't understand thanks youuuuu.