Recursion
// Importing the "fmt" package, which provides functions for formatted I/O.
import "fmt"
// Function fact calculates the factorial of an integer using recursion.
func fact(n int) int {
// Base case: factorial of 0 is 1.
if n == 0 {
return 1
}
// Recursive case: n! = n * (n-1)!
return n * fact(n-1)
}
// The main function, which serves as the entry point for the program.
func main() {
// Printing the factorial of 7 using the fact function.
fmt.Println(fact(7))
// Declaring a variable fib as a function type that takes an integer parameter and returns an integer.
var fib func(n int) int
// Assigning a recursive anonymous function to the fib variable.
fib = func(n int) int {
// Base cases: Fibonacci of 0 is 0, and Fibonacci of 1 is 1.
if n < 2 {
return n
}
// Recursive case: fib(n) = fib(n-1) + fib(n-2)
return fib(n-1) + fib(n-2)
}
// Printing the 7th Fibonacci number using the fib function.
fmt.Println(fib(7))
}Output
Last updated