Tuesday, December 11, 2018

Golang Recursion - Recursive function with examples

In this blog post, we are going to learn Recursive functions in golang with examples.

golang recursive function

Recursion is general programming code process in which method or function calls itself continuously. Go Language support recursion using functions. In Golang, function call itself is called recursive function. Using this technique, It allows code simple but complex to understand the code.

 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

Syntax

// 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. 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 block executes recursive calls, else block will break the recursive call

Golang Recursion function Example: Infinite times 

function recursivefunction is declared. This will be called inside the main function during a normal first call. recursivefunction() is called inside same function. This will be executed infinite times. We have to issue a manual CTRL +C keyword to exit from the program execution
package main

import (
 "fmt"
)

func recursivefunction() {
 fmt.Println("welcome")
 recursivefunction()
}

func main() {
 recursivefunction()
}
Output is
welcome
welcome
welcome
.....
infite times

Golang Recursion function Example: finite times

In the below example 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 exit from the recursive function. if 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

Recursion function Example generates Factorial Number 

A recursive function is declared to calculate a factorial. This return factorial of a 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 as 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

Related article


EmoticonEmoticon