Recursion & Iteration

Recursion

S.P.P.S. - SPPS Provides Pleasing Services
            ^
            |
         S.P.P.S. - SPPS Provides Pleasing Services
                        ^
                        |
                     S.P.P.S. - SPPS Provides Pleasing Services
                                 ^
                                 |
                               S.P.P.S. - ...

Recursion is a definition referring to itself - "opening" it creates infinite depth. In programming, it is a function that calls itself. The recursion "dives deeper and deeper" until a terminating condition is met.

unsigned int factorial(unsigned int x)
{
  if (x > 1)
    return x * factorial(x - 1); // recursive call
  else
    return 1; // terminating condition
}

Iteration

Iteration repeats the same process but does not call itself. Iterative implementation of above function:

unsigned int factorial(unsigned int x)
{
  unsigned int result = 1;
  
  while (x > 1)
  {
    result *= x;
    x--;
  }

  return result;
}