What is recursion ?
Laura Callejas
Bilingual Web Developer | Front End Developer | JavaScript | HTML5 | CSS3 | Figma | Sass | React
Is the process or concept in programming where the function calls itself, we use recursion cause for certain problems it will give you a better solution, more effective and you are able to reuse code, lest's see the following example to solve pow.
In c languages for example we need to iterate to accomplish several multiplications, so you do it through a while or for loop, so that means we can use recursion, before we start we need to understand that the power of a number represents the amount of times the number itself needs to be multiplies, three power of two is equal to 2 ^ 3 = 2*2*2.
In order to see how this works inside memory, we are going to talk about the stack , in the following imagen we can se how is our program and variables are located at .
So float x and float y , will be located at Static Variables space, that means every time we make a call of the function _pow_recursion() those variables with the new value are going to be there , as well the code .
Les't now see who it works we going to assign the value of 3 to x and the value of 2 to y,
x = 3 y = 2 now, when the function stops to calls itself ? well when the variable y reaches zero.
领英推荐
Now what happen if our float y is negative y = -2 , so we will call the function with the following line : _pow_recursion(x, y + 1) / x; is the same process but this time instead to subtract it, We will de adding it (y + 1), every time we do a new call it creates on the heap new variables with the new value , and once it hits the return on the last line they are delete and used to computing the new value.
Thanks to share with me your learning path, or just have a good time refreshing info
I hope you enjoy it, see you on the next one !