Related Articles
Different Types of Recursion in Golang
• Last Updated : 10 Jul, 2020

Recursion is a concept where a function calls itself by direct or indirect means. Each call to the recursive function is a smaller version so that it converges at some point. Every recursive function has a base case or base condition which is the final executable statement in recursion and halts further calls.

There are different types of recursion as explained in the following examples:

### 1. Direct Recursion

Type of recursion where the function calls itself directly, without the assistance of another function is called a direct recursion. The following example explains the concept of direct recursion:

Example:

 `// Golang program to illustrate the``// concept of direct recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// recursive function for ``// calculating a factorial ``// of a positive integer``func factorial_calc(number ``int``) ``int` `{`` ` `    ``// this is the base condition``    ``// if number is 0 or 1 the``    ``// function will return 1``    ``if` `number == 0 || number == 1 {``        ``return` `1``    ``}``     ` `    ``// if negative argument is ``    ``// given, it prints error``    ``// message and returns -1``    ``if` `number < 0 {``        ``fmt.Println(``"Invalid number"``)``        ``return` `-1``    ``}``     ` `    ``// recursive call to itself``    ``// with argument decremented ``    ``// by 1 integer so that it``    ``// eventually reaches the base case``    ``return` `number*factorial_calc(number - 1)``}`` ` `// the main function``func main() {`` ` `    ``// passing 0 as a parameter``    ``answer1 := factorial_calc(0)``    ``fmt.Println(answer1, ``"\n"``)``     ` `    ``// passing a positive integer``    ``answer2 := factorial_calc(5)``    ``fmt.Println(answer2, ``"\n"``)``     ` `    ``// passing a negative integer``    ``// prints an error message``    ``// with a return value of -1``    ``answer3 := factorial_calc(-1)``    ``fmt.Println(answer3, ``"\n"``)``     ` `    ``// passing a positive integer``    ``answer4 := factorial_calc(10)``    ``fmt.Println(answer4, ``"\n"``)``}`

Output:

```1

120

Invalid number
-1

3628800
```

### 2. Indirect Recursion

The type of recursion where the function calls another function and this function, in turn, calls the calling function is called an indirect recursion. This type of recursion takes the assistance of another function. The function does call itself, but indirectly, i.e., through another function. The following example explains the concept of indirect recursion:

Example:

 `// Golang program to illustrate the``// concept of indirect recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// recursive function for ``// printing all numbers ``// upto the number n``func print_one(n ``int``) {``     ` `    ``// if the number is positive``    ``// print the number and call ``    ``// the second function``    ``if` `n >= 0 {``        ``fmt.Println(``"In first function:"``, n)``        ``// call to the second function``        ``// which calls this first``        ``// function indirectly``        ``print_two(n - 1)``    ``}``}`` ` `func print_two(n ``int``) {`` ` `    ``// if the number is positive``    ``// print the number and call ``    ``// the second function``    ``if` `n >= 0 {``        ``fmt.Println(``"In second function:"``, n)``        ``// call to the first function``        ``print_one(n - 1)``    ``}``}`` ` `// main function``func main() {``     ` `    ``// passing a positive ``    ``// parameter which prints all ``    ``// numbers from 1 - 10``    ``print_one(10)``     ` `    ``// this will not print``    ``// anything as it does not``    ``// follow the base case``    ``print_one(-1)``}`

Output:

```In first function: 10
In second function: 9
In first function: 8
In second function: 7
In first function: 6
In second function: 5
In first function: 4
In second function: 3
In first function: 2
In second function: 1
In first function: 0
```

Note: An indirect recursion with only 2 functions is called mutual recursion. There can be more than 2 functions to facilitate indirect recursion.

### 3. Tail Recursion

A tail call is a subroutine call which is the last or final call of the function. When a tail call performs a call to the same function, the function is said to be tail-recursive. Here, the recursive call is the last thing executed by the function.

Example:

 `// Golang program to illustrate the``// concept of tail recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// tail recursive function``// to print all numbers ``// from n to 1``func print_num(n ``int``) {``     ` `    ``// if number is still ``    ``// positive, print it``    ``// and call the function``    ``// with decremented value``    ``if` `n > 0 {``        ``fmt.Println(n)``         ` `        ``// last statement in ``        ``// the recursive function``        ``// tail recursive call``        ``print_num(n-1)``    ``}``}`` ` `// main function``func main() {``     ` `    ``// passing a positive ``    ``// number, prints 5 to 1``    ``print_num(5)``}`

Output:

```5
4
3
2
1
```

In a head recursion, the recursive call is the first statement in the function. There is no other statement or operation before the call. The function does not have to process anything at the time of calling and all operations are done at returning time.

Example:

 `// Golang program to illustrate the``// concept of head recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// head recursive function``// to print all numbers ``// from 1 to n``func print_num(n ``int``) {``     ` `    ``// if number is still ``    ``// less than n, call ``    ``// the function``    ``// with decremented value``    ``if` `n > 0 {``         ` `        ``// first statement in ``        ``// the function``        ``print_num(n-1)``         ` `        ``// printing is done at``        ``// returning time``        ``fmt.Println(n)``    ``}``}`` ` `// main function``func main() {``     ` `    ``// passing a positive ``    ``// number, prints 5 to 1``    ``print_num(5)``}`

Output:

```1
2
3
4
5
```

Note: Output of head recursion is exactly the reverse of that of tail recursion. This is because the tail recursion first prints the number and then calls itself, whereas, in head recursion, the function keeps calling itself until it reaches the base case and then starts printing during returning.

### 5. Infinite Recursion

All the recursive functions were definite or finite recursive functions, i.e., they halted on reaching a base condition. Infinite recursion is a type of recursion that goes on until infinity and never converges to a base case. This often results in system crashing or memory overflows.

Example:

 `// Golang program to illustrate the``// concept of infinite recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// infinite recursion function``func print_hello() {``     ` `    ``// printing infinite times``    ``fmt.Println(``"GeeksforGeeks"``)``    ``print_hello()``}`` ` `// main function``func main() {``     ` `    ``// call to infinite ``    ``// recursive function``    ``print_hello()``}`

Output:

```GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
..... infinite times
```

### 6. Anonymous Function Recursion

In Golang, there is a concept of functions which do not have a name. Such functions are called anonymous functions. Recursion can also be carried out using anonymous functions in Golang as shown below:

Example:

 `// Golang program to illustrate ``// the concept of anonymous ``// function recursion``package main`` ` `import (``    ``"fmt"``)`` ` `// main function``func main() {``     ` `    ``// declaring anonymous function``    ``// that takes an integer value``        ``var anon_func func(``int``)``          ` `    ``// defining an anonymous``    ``// function that prints``    ``// numbers from n to 1``        ``anon_func = func(number ``int``) {``     ` `        ``// base case``            ``if` `number == 0 {``                    ``return` `            ``} ``else` `{``            ``fmt.Println(number)``             ` `            ``// calling anonymous ``            ``// function recursively``                    ``anon_func(number-1)``            ``}``        ``}``     ` `    ``// call to anonymous ``    ``// recursive function``        ``anon_func(5)``}`

Output:

```5
4
3
2
1
```
My Personal Notes arrow_drop_up