A tail recursive method is efficient for reducing stack size. A recursive method is said to be tail recursive if there are no pending operations to be performed on return from a recursive call, as illustrated in Figure below (a). However, method B in Figure below (b) is not tail recursive because there are pending operations after a method call is returned.
is tail recursive because there are no pending operations after recursively invoking isPalindrome in line 15. However, the recursive factorial method (lines 17–22) in is written in a tail-recursive way in the code below.
The first factorial method (line 5) simply invokes the second auxiliary method (line 6). The second method contains an auxiliary parameter result that stores the result for the factorial of n. This method is invoked recursively in line 14. There is no pending operation after a call is returned. The final result is returned in line 12, which is also the return value from invoking factorial(n, 1) in line 6.

SOCIAL SHARE CARD GENERATOR