Recursion is less efficient than a loop. Therefore use a loop instead of recursion whenever you can. Recursion usually helps create cleaner code. Therefore use recursion when clarity is needed more than efficiency. Consider the following example, which calculates the nth term in the Fibbonacci series. The recursive function calls itself 21,890 times to calculate the 20th term whereas the non-recursive function goes through the loop only 19 times.
Using recursion:
int fibbo ( int n )
{
if ( n < 2 )
return ( n ) ;
return ( fibbo ( n – 1 ) + fibbo ( n ) ) ;
}
Without recursion:
int fibbo ( int n )
{
int a = 0, b = 1, c ;
if ( n < 2 )
return ( n ) ;
while ( 2 <= n– )
{
c = a + b ;
a = b ;
b = c ;
}
return ( c ) ;
}