Both algorithms repeatedly execute a set of instructions. Recursion is a function that call itself repeatedly. Iteration is when a loop is executed repeatedly while certain condition is true.

Differences between recursion and iteration:

  • Recursion is a process applied on a function, iteration does not require a function.
  • Infinite recursion can lead to system crash (stack overflow). Infinite iteration will just consume CPU cycles.
  • Termination statement in recursion is included in the body of the function, while termination of iteration is part of the loop condition.
  • Recursion uses the stack to store local variables and parameters each time the function is called. Iteration may not use the stack (it depends on its placement in the code).
  • Recursion reduces the size of  the code.
  • Recursion is usually slower than iteration, because all function calls must be stored in a stack . 

Which to choose?

There is no clear answer. If you ask mathematicians they will say that recursion makes the solution shorter and closer in nature to mathematics. If you ask software developers they will prefer iteration and will say that it is more appealing due to the control that stays local to loop (less “magical”).

Was this article helpful?