tail recursion

Some subroutines/functions end by calling another function. For example, the last statement in a function named “foo” may be to invoke/call a function named “bar.” When the called function (bar) returns, the original function (foo) immediately completes and returns. Any value returned by the called function (bar) will be used as the return value from the caller (foo). This pattern often occurs in functional programs, where recursion is used to implement repetitive looping behaviour. Without special attention, some functional programs that use recursion in this way can create very deep stacks by pushing thousands of return addresses onto the stack. 
A common optimization is called the “tail recursion optimization”. Here is how it works. The compiled code for the caller (foo) is modified to not actually call bar, but instead to simply jump to the first instruction in bar. Then, when bar returns, the processor will effectively return from foo, since no other return address was saved. Furthermore, anything returned by bar will be naturally be returned to foo’s caller. The tail recursion optimization effectively turns functional programs which use recursion to perform looping activities into instruction sequences that use “goto” instructions. It coverts recursive programs into looping programs with “goto” instructions, making recursive programming techniques practical. Quite often a recursive function will call itself. In such cases of the tail recursion optimization, the “goto” need not be a long-distance jump and the J instruction will suffice. But in other cases, mutual recursion might involve separately compiled functions and require the long-distance jumping ability of the TAIL instruction.