(Go Lang) Recursion (GBEN)

Go by Example

Example

  • It has it

package main

import "fmt"

// This fact function calls itself until it reaches the base case of fact(0).
func fact(n int) int {
  if n == 0 {
    return 1
  }
  return n * fact(n-1)
}

func main() {
  fmt.Println(fact(7))
}
5040

Attribution

Leave a Reply

Your email address will not be published. Required fields are marked *