Share your knowledge — Recursion
What is recursion?
Well recursion in computer science is a function that call’s itself until a base condition is met.
for example lets take this function :
`float _pow_recursion(float x, float y)
{
if (y == 0)
return (1);
if (y < 0)
return (_pow_recursion(x, y + 1) / x);
return (_pow_recursion(x, y - 1) * x);
`
}
In this our base condition here is if (y == 0)
if this condition is met the loop will stop.
This condition is necessary because in its absence we will call our function infinitely.
now to the looping part, when we call this function in the
return (_pow_recursion(x, y - 1) * x);
we do so with a twist , we will do so increment or decrement y by 1 in order to reach 0 and thus approaching our base condition (y == 0) .
this is most similar to the Russian dolls, every doll holds a similar yet smaller doll inside (y — 1 in our case) .
but finally when we meet our condition what happens, well, we return the value of course , what value exactly well when you == 0 is reached we return 1 this value is then added to the previous return witch will divide it by x and send it to the higher function until we reach the first instance we called the function .
so if or function loops five times the final return would be something like this ( ( ( ( (1 / x) / x) / x) / x), stacked similarly to the dolls inside one another .
it’s basically inception the function.