C / C ++需要本地函数原型吗?(C/C++ need for local functions prototypes?)

在C / C ++中显式原型本地函数是否有任何优势,而不是在使用之前定义函数? 通过本地我的意思是函数只在源文件中使用whith。 一个例子是:

#include "header.h" static float times2(float x){ return 2*x; } static float times6(float x){ return times2(3*x); } int main(void){ // Other stuff ... float y = times6(1); // Other stuff ... }

对此:

#include "header.h" // Local function prototypes static float times2(float); static float times6(float); // Main int main(void){ // Other stuff ... float y = times6(1); // Other stuff ... } // Local functions definition static float times2(float x){ return 2*x; } static float times6(float x){ return times2(3*x); }

我个人更喜欢使用第一个选项,因为编写的代码较少,(对我来说)文件更容易阅读,但现在我想知道是否有任何技术理由更喜欢第二个选项。

编辑:我添加静态到times2()和times6(),请参阅@Gangadhar回答和评论。

Is there any advantage on explicitly prototype local functions in C/C++, instead of defining the functions before use? By local I mean functions only used whithin their source file. An example is this:

#include "header.h" static float times2(float x){ return 2*x; } static float times6(float x){ return times2(3*x); } int main(void){ // Other stuff ... float y = times6(1); // Other stuff ... }

Versus this:

#include "header.h" // Local function prototypes static float times2(float); static float times6(float); // Main int main(void){ // Other stuff ... float y = times6(1); // Other stuff ... } // Local functions definition static float times2(float x){ return 2*x; } static float times6(float x){ return times2(3*x); }

Personally I prefer to use the first option as there is less code to write and (for me) the file is easier to read, but now I wonder if there is any technical reason to prefer the second option.

EDIT: I added static to times2() and times6(), see @Gangadhar answer and comments following.

最满意答案

在某些情况下,您需要事先声明函数原型,即编译器需要在能够使用该函数之前知道函数原型。

考虑这些没有特别有用的功能:

int foo(int x) { if(x < 1) return x; else return x + bar(x-1); } int bar(int x) { if(x < 3) return x; return x * foo(x-1); }

如果你试图编译它,编译器会生你的气:

错误:未在此范围内声明“bar”

您需要使用它将缺少的原型放在函数前面:

int bar(int); // as above unchanged

这是编译器要求您在函数之前放置函数原型的唯一情况。 在所有其他情况下,将原型随意放置在任何地方是完全合法的,因此这也是合法的:

int foo(int); int bar(int); int foo(int); int bar(int);

虽然显然多余(请不要这样做)。 有些人认为将每个函数的函数原型放在文件顶部的文件中是个好方法,因为

您不必关心编译器要求您再执行此操作的情况,并且有些人显然无法解释编译器的错误消息(我认为这非常简洁),并且 您可以在一个视图中看到文件提供了哪些功能以及如何调用它们。

但这就是风格讨论。 我喜欢让我的代码尽可能短,所以我只会使用编译器所需的原型,但这纯粹是品味和惯例的问题。 在罗马时,像罗马人那样做。

There are cases when you need to declare the function prototype beforehand, i.e. the compiler needs to know a functions prototype before you are able to use that function.

Consider these functions that do nothing particularly useful:

int foo(int x) { if(x < 1) return x; else return x + bar(x-1); } int bar(int x) { if(x < 3) return x; return x * foo(x-1); }

If you try to compile this, the compiler gets mad at you:

error: 'bar' was not declared in this scope

You need to put the missing prototypes in front of the function using it:

int bar(int); // as above unchanged

This is the only case where the compiler requires you to put a function prototype before your functions. In all other cases it is perfectly legal to put prototypes anywhere as often as you'd like, so this is also legal:

int foo(int); int bar(int); int foo(int); int bar(int);

Though obviously redundant (please don't do that). Some people consider it good style to put a function prototype of every function within a file at the top of the file, because

you don't have to care about cases in which the compiler requires you to do it anymore, and some people apparently cannot interpret the error message by the compiler (which I think is very concise), and you see on one view what functions are provided by the file and how they are called.

But this is exactly that, a style discussion. I like to keep my code as short as possible, so I would only use the prototypes that are required by the compiler, but that's purely a matter of taste and convention. When in Rome, do as the Romans or something like that.

更多推荐