function tailrecsum(x, running_total=0) {
if (x===0) {
return running_total;
} else {
return tailrecsum(x-1, running_total+x);
}
}
In this example, we see that the result will yield:
tailrecsum(5, 0)
tailrecsum(4, 5)
tailrecsum(3, 9)
tailrecsum(2, 12)
tailrecsum(1, 14)
tailrecsum(0, 15)
15
In such a case, isn't there a new stack every time tailrecsum is called? And isn't tailrecsum(5, 0)
waiting for the next tailrecsum
to finish, hence not closing that stack yet?