Recursion for dummies

Recursion is the process of repeating an act in a self-similar way. It is highly used in programming for it’s ability to break down problems. To understand the term Recursion more let’s break it down using an Example:

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

The given algorithm calculates a number x taken to the power of y:
let’s make x = 3and y = 3. _pow_recursion(3, 3)

The second line checks to see if y is 0. If y is 0 we don’t have to go all the way and break down the problem into smaller pieces anymore. This is known as the base case. However, If we’ve reached the smallest the problem needs to be broken down into, it returns 1.

The next line will only gets called if the second line didn’t return 1. And in this case we can go ahead and break down the problem into smaller pieces (return (_pow_recursion(x, y + 1)/ x);)until we reach the base case. This is known as the smaller caller. Now the function will fist call the power(3, 3) and checks if it’s equal to 0 and that’s not the case it goes to the next line (3,2) and checks it then goes to (3, 1) and checks it and finally to (3, 0) and that equals 0 so now the function is completed and the next step is for it to gets popped off the stack. (return (_pow_recursion(x, y - 1)* x);)