# 函数原型

函数原型（function prototype）是对函数的声明，它指明函数名、返回类型以及各参数的类型（也可选择性地给出参数标识符），但不含函数体。在 C 和 C++ 中，原型的形式与函数定义相同，只是在右括号之后紧接一个分号即告结束，因此编译器无法由它生成机器码。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup> 原型使编译器能够检查对该函数的每一次调用是否存在实参类型不匹配或返回类型不匹配的情况，包括出现在定义之前或其他源文件中的调用。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup>

| 关键事实 | 说明 |
|---|---|
| 定义 | 一种函数声明，确立函数的返回类型与各参数的类型（也可选择性地给出标识符），但没有函数体<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup> |
| 形式 | 与定义的头部相同，以分号结束；参数名可以与定义中的相同、不同，或完全省略<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup><sup> • </sup><sup>[2](https://icarus.cs.weber.edu/~dab/cs1410/textbook/6.Functions/def_and_dec.html)</sup> |
| 用途 | 强制把实参转换为所声明的参数类型，并支持在编译期检查实参的数目与类型<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup> |
| 名字的作用域 | 原型中的参数标识符具有函数原型作用域，止于函数声明符的末尾<sup>[4](https://learn.microsoft.com/en-us/cpp/c-language/scope-and-visibility)</sup> |
| 历史 | ANSI C（X3J11）借用 C++ 的语法，把形参类型纳入函数的类型签名<sup>[5](https://csapp.cs.cmu.edu/public/docs/chistory.html)</sup> |
| C23 的变化 | 旧式（K&R）函数声明与定义被删除；声明中的空参数列表现在表示 `void`<sup>[6](https://en.cppreference.com/c/23)</sup><sup> • </sup><sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup> |

## 声明与定义

函数定义由头部和函数体组成；编译器把函数体翻译成机器码，程序在运行时将其存放在内存中。原型则是函数头部后接一个分号，因而是一条没有函数体的语句。两者都不可或缺：定义提供代码，声明则把函数名引入编译器的符号表，以便此后检查各次调用。<sup>[2](https://icarus.cs.weber.edu/~dab/cs1410/textbook/6.Functions/def_and_dec.html)</sup> 没有函数体的声明只是声明了函数，并未定义它。<sup>[7](https://en.cppreference.com/cpp/language/definition)</sup>

原型中的参数名不受定义的约束。它们可以与定义中的名字相同、不同，或完全省略，因为编译器并不检查这些名字；例如 `double foo(int x, double y, char z);`、`double foo(int a, double b, char c);` 和 `double foo(int, double, char);` 三者都是同一个定义的有效原型。<sup>[2](https://icarus.cs.weber.edu/~dab/cs1410/textbook/6.Functions/def_and_dec.html)</sup> 完整的参数声明与抽象声明符甚至可以混用在同一个原型中，如 `int add(int a, int);`。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup>

## 原型为编译器做了什么

新式声明对之后所有的调用表达式都起原型的作用：它强制把实参表达式转换为所声明的参数类型，并支持在编译期检查实参的数目。<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup> 若没有完整的原型，则只施加标准转换，而不会去核对实参的类型或数目是否与参数相符。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup>

这种检查之所以重要，是因为每个参数转换后的类型决定了调用实参如何被解释。不匹配会导致对栈的误读：在 16 位计算机上，把一个 16 位指针作为实参传入，而对应参数声明为 `long`，则栈上的前 32 位会被当作该 `long` 来解释，从而破坏这个参数以及其后的每一个参数。为所有函数声明完整的原型可以查出这类错误。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup>

原型还为返回类型不是 `int` 的函数确立返回类型，并可在函数定义之前用来初始化指向函数的指针。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup> 实践中，原型放在源文件顶部或靠近顶部的位置，位于任何函数之外，通常紧接在 `#include` 指令之后，或者放在头文件中；诸如 `math.h` 这样的库头文件，大部分内容就是 `sqrt()`、`cos()` 之类函数的原型。<sup>[2](https://icarus.cs.weber.edu/~dab/cs1410/textbook/6.Functions/def_and_dec.html)</sup><sup> • </sup><sup>[8](https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/Functions.html)</sup> 与定义不同，函数声明既可以出现在文件作用域，也可以出现在块作用域。<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup>

## 原型参数的作用域

C 承认四种作用域：文件、函数、块和函数原型。在原型参数列表中声明的标识符具有函数原型作用域，该作用域止于函数声明符的末尾；这样的名字不属于函数声明的外层作用域。<sup>[4](https://learn.microsoft.com/en-us/cpp/c-language/scope-and-visibility)</sup> 正因如此，首次在原型内部声明的 `struct` 或 `union` 标记会被登记在这个有限的作用域中，若别处需要该类型，就必须在原型之前于全局作用域中声明它。<sup>[1](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)</sup>

## 旧式声明及其在 C23 中的移除

在 C23 之前，C 还允许使用标识符列表的旧式（K&R）声明与定义，如 `int max(a, b) int a, b; { ... }`。这样的声明不引入原型：其后的调用只进行默认实参提升，而实参数目与参数数目不符的调用会引发未定义行为。<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup> 同样，在 C23 之前，带空括号的声明也不起原型的作用。<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup>

C23 是 C 标准的现行版本（ISO/IEC 9899:2024），它依据提案 N2841 彻底删除了旧式函数声明与定义。<sup>[6](https://en.cppreference.com/c/23)</sup> 自 C23 起，已无法声明一个不带原型的函数，而带空括号的声明现在是新式声明，等价于参数列表为 `void`。<sup>[3](https://en.cppreference.com/c/language/function_declaration)</sup>

## 历史

函数原型随 ANSI 标准化进程进入标准 C。[丹尼斯·里奇](https://www.edgepedia.org/zh/dennis-ritchie)（Dennis Ritchie）所写的该语言史记载，X3J11 委员会对语言本身只做了一项真正重要的改动：借用 C++ 的语法，把形参的类型纳入函数的类型签名。<sup>[5](https://csapp.cs.cmu.edu/public/docs/chistory.html)</sup> 这一特性使程序员能够为函数指定更多信息，以便在编译期间更好地查错；C9X 的理由说明指出，在包含不匹配调用的翻译单元中提供新式声明，可以使不匹配的旧式头部暴露出来。<sup>[9](https://www.sciencedirect.com/topics/engineering/function-prototype)</sup><sup> • </sup><sup>[10](https://pllab.cs.nthu.edu.tw/cs340402/readings/c/c9x_standard.pdf)</sup>

## References

1. [Function Prototypes, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/c-language/function-prototypes?view=msvc-170)
2. [6.2.1. Function Definitions and Declarations, Weber State University CS 1410 textbook](https://icarus.cs.weber.edu/~dab/cs1410/textbook/6.Functions/def_and_dec.html)
3. [Function declarations, cppreference.com (C)](https://en.cppreference.com/c/language/function_declaration)
4. [Scope and Visibility, Microsoft Learn](https://learn.microsoft.com/en-us/cpp/c-language/scope-and-visibility)
5. [The Development of the C Language, Dennis M. Ritchie](https://csapp.cs.cmu.edu/public/docs/chistory.html)
6. [C23, cppreference.com](https://en.cppreference.com/c/23)
7. [Definitions and ODR (One Definition Rule), cppreference.com (C++)](https://en.cppreference.com/cpp/language/definition)
8. [C Programming Course Notes - Functions, University of Illinois Chicago](https://www.cs.uic.edu/~jbell/CourseNotes/C_Programming/Functions.html)
9. [Function Prototype - an overview, ScienceDirect Topics](https://www.sciencedirect.com/topics/engineering/function-prototype)
10. [WG14/N850 C9X Rationale](https://pllab.cs.nthu.edu.tw/cs340402/readings/c/c9x_standard.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 19, 2026; Sep 30, 2026; Oct 25, 2026 · Edited: Sep 19, 2026; Sep 30, 2026; Oct 25, 2026 · Last review: Oct 25, 2026*

Topic: 社会与历史 › 语言与语言学 › 语言学 › Formal and computational linguistics › Concrete syntax of programming and query languages

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/function-prototype

English original: https://www.edgepedia.org/function-prototype
