Recursive Anonymous Function in Golang

Recursion is a process in which a function calls itself implicitly or explicitly and the corresponding function is called recursive function. Go language supports special feature called anonymous function. It is a function that doesn’t contain any name. It is used to create an inline function. Recursive functions can be declared & defined as anonymous also. A recursive anonymous function is also known as recursive function literal.

Syntax:

func(parameter-list)(return-type){
// code..
// call same function
// within the function
// for recursion
// Use return statement only
// if return-type are given.
return
}()

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show
// how to create an recursive
// Anonymous function
package main
  
import "fmt"
  
func main() {
  
    // Anonymous function
    var recursiveAnonymous func()
    recursiveAnonymous = func() {
      
        // Printing message to show the
        // function call and iteration.
        fmt.Println("Anonymous functions could be recursive.")
          
        // Calling same function 
        // recursively
        recursiveAnonymous()
  
    }
      
    // Main calling of
    // the function
    recursiveAnonymous()
  
}

chevron_right


Output:

Anonymous functions could be recursive.
Anonymous functions could be recursive.
Anonymous functions could be recursive.
Anonymous functions could be recursive.
.
.
.
.
Infinite times function calls.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to show
// how to create an recursive
// Anonymous function
package main
  
import (
    "fmt"
)
  
func main() {
  
    // Anonymous function
    var recursiveAnonymous func(int)
      
    // Passing arguments
    // to Anonymous function
    recursiveAnonymous = func(variable int) {
      
        // Checking condition
        // to return
        if variable == -1 {
          
            fmt.Println("Welcome to Geeks for Geeks!")
            return
        } else {
          
            fmt.Println(variable)
              
            // Calling same
            // function recursively
            recursiveAnonymous(variable - 1)
        }
    }
      
    // Main calling
    // of the function
    recursiveAnonymous(10)
}

chevron_right


Output:

10
9
8
7
6
5
4
3
2
1
0
Welcome to Geeks for Geeks!



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.