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:
package main
import (
"fmt"
)
func factorial_calc(number int ) int {
if number == 0 || number == 1 {
return 1
}
if number < 0 {
fmt.Println( "Invalid number" )
return -1
}
return number*factorial_calc(number - 1)
}
func main() {
answer1 := factorial_calc(0)
fmt.Println(answer1, "\n" )
answer2 := factorial_calc(5)
fmt.Println(answer2, "\n" )
answer3 := factorial_calc(-1)
fmt.Println(answer3, "\n" )
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:
package main
import (
"fmt"
)
func print_one(n int ) {
if n >= 0 {
fmt.Println( "In first function:" , n)
print_two(n - 1)
}
}
func print_two(n int ) {
if n >= 0 {
fmt.Println( "In second function:" , n)
print_one(n - 1)
}
}
func main() {
print_one(10)
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:
package main
import (
"fmt"
)
func print_num(n int ) {
if n > 0 {
fmt.Println(n)
print_num(n-1)
}
}
func main() {
print_num(5)
}
|
Output:
5
4
3
2
1
4. Head Recursion
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:
package main
import (
"fmt"
)
func print_num(n int ) {
if n > 0 {
print_num(n-1)
fmt.Println(n)
}
}
func main() {
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:
package main
import (
"fmt"
)
func print_hello() {
fmt.Println( "GeeksforGeeks" )
print_hello()
}
func main() {
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:
package main
import (
"fmt"
)
func main() {
var anon_func func( int )
anon_func = func(number int ) {
if number == 0 {
return
} else {
fmt.Println(number)
anon_func(number-1)
}
}
anon_func(5)
}
|
Output:
5
4
3
2
1
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...