Is every recursive function convertible to iteration? What characteristic should a recursive function have in order for it to be implemented using iteration?
<
Yes, every recursive function can be converted to an iterative one by following a rather mechanical process.
Recall that compilers implement recursion by using a stack, which is typically implemented in the CPU's hardware. You can build a software stack of your own, make it suitable for keeping the state of your function (i.e. its local variables), push the initial state onto that stack, and write a while
loop that pushes new state onto the stack instead of making a recursive call, popping the stack instead of returning, and continuing the process while the stack is not empty.