Recursion: The recursive definition of recursion
Long story short, recursive functions are functions that call themselves.
To give you a stack view of recursion we will take a look at the following 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);
}
As you can see, _pow_recursion calls itself within the code. A recursive function requires 3 distinct parts in order to function properly. Working backwards we have First: An exit condition. Once this condition is met, in the above case when y is 0, the function makes a return without calling itself again, in the above function they return 1. The second and third part are almost always a part of each other, 2nd the recursive call itself, and third something that causes your data to approach the exit condition. In the above program when making the recursive calls, if y is less than 0 the recursive call is on y + 1, and if it is greater than 0 it calls itself passing y minus 1. Both calls serve to drive the data closer to the exit condition. Once this final exit condition is met the stack begins returning, one by one, starting from the latest call ending at the first call. So let us take a look at how this would actually happen on a run time stack:
We will give our initial call the values x=2, and y=4.
As you can see, it begins calling itself over and over again until eventually y is 0, at which point it returns to the function that called it 1, which then returns 1 * 2, or 2 to the function that called it, which returns 2 *2, or 4, all the way to the very first call of the function which would return 8 *2, or 16. And that is recursion. If you want to see another example of recursion just follow this link: https://selidex.medium.com/recursion-the-recursive-definition-of-recursion-7916beb657c0