0
votes

I have been reading a lot of articles about tail recursion.But i am still unclear about how the tail recursion uses no stack space.

What about the local variables and function arguments? They are also stored in the stack.What will happens to these in case of a tail recursion? How is it different from normal recursion.

"A tail call [tail recursion] is a kind of goto dressed as a call"

What this really means?

1

1 Answers

3
votes

Local variables are forgotten. The whole point of tail-call recursion is that it is the last thing that will happen in a function, so local variables won't matter after the call. In fact, there not being an "after the call" is what the tail-call optimisation is all about.

EDIT: About the quote:

In a tail-call-optimised scenario, these two are equivalent:

fn(x) {
  ....
  fn(y);
}

and

fn(x) {
  start:
  ....
  x = y;
  goto start;
}