# Tail delivery

2020-11-07 21:04:16

When the recursive call is the last statement executed in the whole function body and its return value is not part of the expression , This recursive call is tail recursion . --- From baidu baike - Tail recursion .

This is about the tail - The understanding of delivery is related to , Look at the definition of the relevant concepts, said are very abstract , The object-oriented features are deeply revealed in the sentences and lines , Leave a wide range of imagination for readers .

Here according to their own understanding of one-sided interpretation of the next recursive ： Tail recursion does not need to use the next recursive call result to calculate in the caller return sentence .

Follow the most common examples to illustrate ：

``` 1 public int rescuvie(int n) {
2         return n == 1 ? n : n * rescuvie(n - 1);
3     }
4
5     public long tailRescuvie(int acc, int n) {
6         return n == 1 ? acc : tailRescuvie(acc * n, n -1);
7     }
8
9     @Test
10     public void test01() {
11         System.out.println(rescuvie(5));
12         System.out.println(tailRescuvie(1, 5));
13     }```

difference ：

Tail recursive return Objects are the result of the next recursive call , Many times recursively stitched together is return acc, No need to know the result of the next recursive call , direct return The next recursive call result , All the way return Just a matter of ;

Recursive return The object also requires the caller to merge with the result of the next recursive call , So you need to keep the result of every recursive call , The intermediate state .

Let's go back to this sentence ： When the recursive call is the last statement executed in the whole function body and its return value is not part of the expression , This recursive call is tail recursion .

The return value is not part of the expression ： No need to know the result of the next recursive call , direct return The next recursive call result ;

The last statement executed in the whole function body ： Namely return sentence , Of course, I don't know the other return statements , Anyway, all I use is return.