The question in my book is: "Which of the recursive calls (and not the functions!) Is the tail recursive?"
unsigned f(unsigned x)
{
if(x==0) return 1;
else if(x==1) return f(x-1);
else return 3*x + f(x-1);
}
In this example, I assume that the first one is tail recursive and the second is not. However, what happens in the following case, when we also have printf()before and after the recursive call (s)?
void f(unsigned x)
{
if(x==0) return;
else if(x==1) { f(x-1); printf("1"); }
else if(x==2) { printf("2"); f(x-1); }
else f(x-3);
}
Given what I still know about tail recursion, I assumed that the first one is not, and the second and third? I'm wrong, and if so, could you explain how this works?
source
share