User's Manual

66 Avoid Recursive Functions
AMD Athlon Processor x86 Code Optimization
22007E/0November 1999
Avoid Recursive Functions
Avoid recursive functions due to the danger of overflowing the
return address stack. Convert end-recursive functions to
iterative code. An end-recursive function is when the function
call to itself is at the end of the code.
Example 1 (Avoid):
long fac(long a)
{
if (a==0) {
return (1);
} else {
return (a*fac(a–1));
}
return (t);
}
Example 2 (Preferred):
long fac(long a)
{
long t=1;
while (a > 0) {
t *= a;
a--;
}
return (t);
}