Recursion is when a function calls itself. This is called recursion.
- It is a different alternative to implement repetition structures (cycles). Modules make recursive recursive calls are made.
- It can be used in any situation in which the solution can be expressed as a sequence of can be expressed as sequence of moves, steps or moves, steps or transformations governed by a set of unambiguous by set of unambiguous rules.
When we execute a program space is created in the memory that is called stack, since it works as its name says it like a stack (Lifo) then that enters or leaves this stack, because every time that a function is executed a new frame is created in the stack that has all the elements related to this function including the variables that are in it.
That is why when we execute a function and it finishes, we return to the previous function, and that is why when a function finishes its frame it leaves the stack and we return to the previous frame.
Although we have in several functions variables with the same name these are independent.
The same stack of a program is used to go through the different frames of the program and the last frame that enters is the first one that leaves and its result is going to pass to the previous one in order to go contracting the solution from frame to frame.
With the recursion we can build smaller and more elegant solutions but this does not assure us that they will be more efficient since it can occupy more memory or be slower than an iterative solution, in addition that the code can not be clear for all the programmers especially the novices, then it is necessary to think well in which cases it is a good option to use the recursion as a solution to a problem.