Recursion
This Go program demonstrates a simple factorial function and a recursive Fibonacci function.
Output
Now, let's break down the code and explain each part:
Function Declarations:
fact(n int) int
: This function calculates the factorial of an integern
using recursion.
Main Function:
main()
: This is the entry point of the program.fmt.Println(fact(7))
: Calls thefact
function and prints the factorial of 7.Declares a variable
fib
as a function type that takes an integer parameter and returns an integer.fib = func(n int) int { ... }
: Assigns a recursive anonymous function to thefib
variable.fmt.Println(fib(7))
: Calls thefib
function and prints the 7th Fibonacci number.
Last updated
Was this helpful?