Golang - Demonstrate the concept of Anonymous Function Recursion Code Example

The code for Demonstrate the concept of Anonymous Function Recursion

package main

import (
	"fmt"
)

func main() {
    var anonymousFunc func(int)
    
    anonymousFunc = func(value int) {
        // base case
        if value == 0 {
            return
        } else {
        fmt.Println(value)
        
        // calling anonymous
        // function recursively
        anonymousFunc(value-1)
        }
    }

    anonymousFunc(7)
}


/*
Output:
7
6
5
4
3
2
1
*/
Code by IncludeHelp, on March 1, 2023 07:11

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.