为什么函数原型在不需要时包括参数名称? [英] Why do function prototypes include parameter names when they're not required?

查看:475
本文介绍了为什么函数原型在不需要时包括参数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为函数原型必须包含函数的参数及其名称。然而,我只是试试这个:

I always thought that a function prototype must contain the parameters of the function and their names. However, I just tried this out:

int add(int,int);

int main()
{
    std::cout << add(3,1) << std::endl;
}

int add(int x, int y)
{
    return x + y;
}

我甚至试着编译极端的过于谨慎:

And it worked! I even tried compiling with extreme over-caution:

g++ -W -Wall -Werror -pedantic test.cpp

它还工作。所以我的问题是,如果你不需要参数名称在函数原型,为什么这么常见这样做?这有什么目的吗?它与函数的签名有关吗?

And it still worked. So my question is, if you don't need parameter names in function prototypes, why is it so common to do so? Is there any purpose to this? Does it have something to do with the signature of the function?

推荐答案

不,这些没有必要,由编译器。你甚至可以在不同的声明中给他们不同的名字;以下是完全合法的:

No, these are not necessary, and are in fact ignored by the compiler. You can even give them different names in different declarations; the following is entirely legal:

int foo(int bar);
int foo(int biz);
int foo(int qux) {
    ...
}

放入它们的原因是文档:

The reason to put them in is documentation:


  • 如果有人读取您的头文件,他们可以一眼就知道每个参数

  • 如果您使用一个奇特的IDE,它可以在您开始输入函数调用时显示参数名称。

  • Doxygen可以解析参数名称并在文档中显示。

这篇关于为什么函数原型在不需要时包括参数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆