> For the complete documentation index, see [llms.txt](https://golang.bagwanpankaj.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://golang.bagwanpankaj.com/recursion.md).

# Recursion

A recursive function is a function that call itself unless it reaches matches a certain condition.&#x20;

{% hint style="warning" %}
A breakpoint for recursive function is necessary otherwise it could keep calling itself for indefinite times, growing heap and stack that could be detrimental to program's performance
{% endhint %}

We are going to implement a classic example to calculate factorial or given number using Go, with recursive methodology

```go
package main
import "fmt"
func factorial(n int) int {
    if n == 0 {
        return 1
    }
    return n * factorial(n-1)
}
func main() {
    fmt.Println(factorial(5))
}
// 120
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://golang.bagwanpankaj.com/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.
