Learn Recursive function with examples in Go language
- Admin
- Dec 31, 2023
- Golang Golang-examples
In this blog post, we are going to learn Recursive functions in golang with examples.
What is the golang recursive function
Recursion
is a general programming code process in which the method or function calls itself continuously.
Go Language supports recursion
using functions. The function call itself is called a recursive function.
Using this technique, Code is simple to write but complex to understand.
Important key points of recursion function.
- Function called inside the same function
- It is always good to specify an exit condition in the recursive function
- It reduces unnecessary function calls and code lines
- Disadvantages with recursion is code is logical and difficult to debug and inspect the code
Recursion Syntax in Golang
// function declaration
func recursefunction(){
//Code statements
recursefunction();//function call itself
}
func main(){
recursefunction() // Initialize first normal call
}
In the above syntax,
- the recursive function is being called inside the same function.
- The recursion process continues execution until the condition is met.
- Otherwise, function executions in infinite recursion
- We can prevent infinite recursion using if-else statements, if the block executes recursive calls, the else block will break the recursive call
Golang Recursion function Example: Infinite times
The function recursivefunction
is declared. It calls inside the main function during a normal first call. recursivefunction()
is called inside the same function. It executes infinite times. We have to issue a manual CTRL +C keyword to exit from the program execution.
Here is an example for Recursive infinite times example
package main
import (
"fmt"
)
func recursivefunction() {
fmt.Println("welcome")
recursivefunction()
}
func main() {
recursivefunction()
}
Output is
welcome
welcome
welcome
.....
infinite times
Golang Recursion function Example: finite times
In the below example a recursive function is declared and called inside the main function at first during the normal first function call. The same function is called recursively inside the same function. This recursive call executes 10 times and exits from the recursive function. If the statement is used to exit from the function.
package main
import (
"fmt"
)
var count = 0
func recursivemethod() {
count++
if count <= 10 {
fmt.Println(count)
recursivemethod()
}
}
func main() {
recursivemethod()
}
Output is
1
2
3
4
5
6
7
8
9
10
How to generate Factorial Number using Recursion function Example?
A recursive function is declared to calculate a factorial
. It returns the ‘factorial` of a given input number.
Here is a program code for calculating the factorial number.
package main
import "fmt"
func factorial(numb int) int {
if numb == 0 {
return 1
}
return numb * factorial(numb-1)
}
func main() {
result := factorial(5)
fmt.Print("Output for Factorial of 5 :", result)
}
Output is
Output for Factorial of 5:120
Recursion example for calculating Fibonacci series
The following program calculates and returns the Fibonacci series for a given input number using a recursive function.
package main
import "fmt"
func fibonacci(value int) int {
if value == 0 || value == 1 {
return value
}
return fibonacci(value-2) + fibonacci(value-1)
}
func main() {
result := fibonacci(10)
fmt.Print("Output for Fibonacci series of 10 :", result)
}
Output is
Output for Fibonacci series of 10:55
Recursive anonymous function
The anonymous function is a function without a name. Recursive functions can be declared anonymous also. Following is example program for Recursive anonymous function in golang
package main
import "fmt"
func main() {
var myfunction func()
myfunction = func() {
fmt.Println("Anonymous function example")
myfunction()
}
myfunction()
}
Output is
Anonymous function example
Anonymous function example
...
...
...
infinite times
Conclusion
Learned recursive functions declaration and syntax and examples for finite and infinite time execution. It also included calculating factorial and Fibonacci series for a given number using a recursive function.