> For the complete documentation index, see [llms.txt](https://docs.1l.lol/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.1l.lol/functions/function-declaration.md).

# 函数的声明与定义

### 结论

函数**不是必须定义在调用之前**，但**必须让编译器在调用之前“见过”它的声明**。

> 一句话：**声明要在调用之前；定义可以放在后面。**

### 1. 定义在前（可以）

```cpp
#include <iostream>
using namespace std;

int add(int a, int b) {   // 定义在调用之前
    return a + b;
}

int main() {
    cout << add(3, 5);    // OK
    return 0;
}
```

### 2. 先声明、后定义（也可以）

```cpp
#include <iostream>
using namespace std;

int add(int a, int b);    // 函数声明（原型），只告诉编译器长什么样

int main() {
    cout << add(3, 5);    // OK，编译器已知道 add 存在
    return 0;
}

int add(int a, int b) {   // 定义放在后面
    return a + b;
}
```

### 3. 什么都没声明就调用（报错）

```cpp
int main() {
    cout << add(3, 5);    // 编译错误：'add' was not declared in this scope
    return 0;
}
int add(int a, int b) { return a + b; }
```

编译到调用处时，编译器还不知道 add 是什么，所以报错。

### 声明 vs 定义

|        | 写法                              | 次数限制             |
| ------ | ------------------------------- | ---------------- |
| 声明（原型） | `int add(int a, int b);`        | 可以多次             |
| 定义     | `int add(int a, int b) { ... }` | 整个程序只能一次（ODR 规则） |

声明可以省略参数名：`int add(int, int);`

### 常见情形

* **递归**：函数在自己的函数体里调用自己没问题，因为函数名在体内已可见。
* **相互调用**（A 调 B、B 又调 A）：至少有一个要先声明，通常两个都前置声明。

  ```cpp
  void b();                 // 前置声明
  void a() { b(); }
  void b() { a(); }
  ```
* **头文件**里一般放**声明**，**定义**放 `.cpp`。
* **类的成员函数**在类内直接写函数体，等于声明+定义，不受前后顺序影响。
* **库函数**：只要 `#include` 了含声明的头文件即可，定义在库里。

### 小结

* 必须：**调用前有声明**。
* 不必须：**定义在调用前**。
* 想省心就把所有函数原型集中写在文件顶部，或先定义再使用。


---

# 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://docs.1l.lol/functions/function-declaration.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.
