What is Recursion?

Julioarenas
5 min readJul 4, 2021

If you are here to find out about the meaning of recursion you came to the right place, and not because I will explain what recursion is, but because from here you will get the fundamental concept of how recursion is done, how to differentiate it from a cycle and why it is not worth using it every time, let’s get down to action:

In certain science what we have to understand that to apply a solution to several problems, is that those problems must have the same characteristics as the initial problem so that the solution of the primary problem can be applied, it is there where we can apply the recursion since we use the solution of the primary problem to apply it to a secondary solution, but if you are asking yourself how can I relate this with the code? What would be the initial solution, because you make solutions all the time and those solutions are called functions, and you think, do I have to call several times the same function to solve another problem? And you think “I already do that”, and that is where I tell you that if you have to call the function, but it is not what you think, because the one you are thinking about is outside the code, that is, you are calling it outside the code of the function.

I am referring that you call it within the code of the function, if so, a function that calls itself, that is to say, that the solution that you propose will be called several times to be able to solve a problem in different versions, but are you going to say that it will become infinite? To solve this we do it by means of an exit condition, this exit condition is called base case, that is to say, that when the base case is fulfilled the function stops calling itself and leaves the function, but let’s talk more about this base case, and how does it work?

Example

How is everything solved? if the base case is an if where we have to fulfill that condition, but that condition depends on the internal operation, that the function does when it solves before calling itself, therefore in the resolution, we have to fulfill that condition of the if to be able to leave the recursive function, then that condition will not be solved, because it will depend on the other solution of the same condition, but then how does it work you ask? it is something very simple, that function has to open a space in memory to make an instance, that is, it has to open a copy of itself in another part of the RAM memory with the current solution of the base case, for which we are based on a mathematical process of factorial decomposition, that is to say, that the result of the multiplication of a certain number is multiplied by all its previous numbers excluding the zero, in other words, the recursion in computer science we apply it to the mathematical recursion doing that a number “N” is discounted continuously by 1 but when discounting N successively and to make a similarity with factorial it is going to arrive to the moment that N is worth 1, and it is going to realize that 1 is the answer of 2 for which it begins to be returned and two is the answer 3 and that 3 is the answer of 4 and four is the answer of 5, assuming that the initial value of N is 5, when N reaches its initial value it returns its answer leaving the function.

RECURSIVITY VS. CYCLES

Well, we have to see a recursion is a function that is called itself, to say a function means that it is a solution that we propose in a part of the code, and here is the first difference with a cycle that while a function is thought in a solution a cycle is thought in repetition, you are going to tell me that the cycle can also execute instructions yes, but at the end, those intrusions are going to depend on or are inside a function, in addition, to make a similarity between the two would be in a very wide way and so much its syntax as its meaning changes too much to do it, in conclusion, a cycle we use it to iterate a certain amount of instructions while a recursion we apply a simple solution N number of times to certain problem.

RECURSION AND MEMORY

To apply the recursion, although it looks simple and aesthetically well in code, it is very expensive, if you remember what I had spoken previously that a recursive function makes a copy of itself or an instance to be able to carry it out, then we have the idea that if I make a copy this has to be stored in a space in memory so that it can exist and as you know we have a limited memory space in which all the programs of our computer are executed, so we can not fill that memory with our recursive function, since it would collapse our operating system since it does not have space where to execute the other programs.

If you have comments or something additional to contribute, write me, thank you for visiting my blog.

--

--