Recursion: A Picture is Worth 1,500 Words

We will explain how works this code in the next drawing:

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);

Our program calls this function recursively,where each call to this function is added to the stack. The stack is a LIFO structure, like a box piled on top of another box where the last one that is put is the first one that is removed. That’s what happens with each call to that function that was added to the stack.

Let’s look at an example using the function with a value x equal to 2 and y equal to three (2³):



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store