> For the complete documentation index, see [llms.txt](https://go.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.netdpi.net/functions/recursive.md).

# 07.05-遞迴（Recursive）

函式可以呼叫它自己，這裡是一個計算費伯納係數（factorial of a number）的方式：

```go
func factorial(x uint) uint {
    if x == 0 {
        return 1
    }

    return x * factorial(x-1)
}
```

`factorial` 呼叫它自己，使得函式進行遞迴（recursive），為了更了解此函式的運作，我們剖析 `factorial(2)` 的運作步驟：

* `x == 0` 成立嗎？否，（`x` 為 2）
* 找出 (2-1) 的費伯納係數
  * `x == 0` 成立嗎？否，（`x` 為 1）
  * 找出 (1-1) 的費伯納係數
    * `x == 0` 成立嗎？是，傳回 1。
  * 傳回 `1 * 1`
* 傳回 `2 * 1`

Closure 與遞迴是強大的程式設計技術，是所謂函式化程式設計（functional programming）的範例基礎。大多數的人會覺得函式化的程式設計比較難以理解，而覺得使用迴圈、if 陳述句、變數與簡易的函式會比較容易。


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://go.netdpi.net/functions/recursive.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
