On Fri, Dec 20, 2013 at 8:04 AM, gamma_chen <[email protected]> wrote:
> The following recursive example cannot generate "tail call". It generate
> "call" only. Anyone knows how to make "tail call"?
>
> // clang -c 1.c -emit-llvm -o 1.bc
> // llvm-dis 1.bc -o -
>
> // File 1.c
> int factorial(int x)
> {
>   x--;
>   if (x <= 0)
>     return 1;
>   return x*factorial(x-1);

This function is not tail-recursive.  A tail-recursive variant would
be something like:

int factorial(int x, int accum) {
  if (x == 1)
    return accum;
  else
    return factorial(x-1, accum*x);
}

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <[email protected]>*/
_______________________________________________
cfe-users mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users

Reply via email to