# Recursion

This Go program demonstrates a simple factorial function and a recursive Fibonacci function.

```go
// 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

```
040
13
```

Now, let's break down the code and explain each part:

1. **Function Declarations:**
   * `fact(n int) int`: This function calculates the factorial of an integer `n` using recursion.
2. **Main Function:**
   * `main()`: This is the entry point of the program.
   * `fmt.Println(fact(7))`: Calls the `fact` 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 the `fib` variable.
   * `fmt.Println(fib(7))`: Calls the `fib` function and prints the 7th Fibonacci number.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/16-recursion.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
