在函数声明中包含参数名称与不包含参数名称有什么区别?

发布于 2024-10-01 20:31:51 字数 554 浏览 7 评论 0原文

我记得之前读过关于在函数声明中包含参数名称和不包含参数名称之间的重要性(或缺乏重要性)的内容。但我不记得我读过什么或在哪里读过。

例如,

void do_something(int *); // No parameter name included, only type.

vs...

void do_something(int * i); // type AND parameter name included.

那么这两个声明有什么区别呢?感谢您阅读并回答这个可能微不足道的问题。

-- 更新 --

好吧,我读到的是我的一位老教授的一套风格指南,警告不要在函数定义中包含参数名称并且不要在函数中使用参数。

void do_something(int * i) { //code that doesn't use i;} //BAD
void do_something(int *) { //code that doesn't use i;} //OK

I remember reading before about the significance (or lack thereof) between including a parameter name in a function declaration and not including one. But I can't remember what it was that I read or where I read it.

for example,

void do_something(int *); // No parameter name included, only type.

vs...

void do_something(int * i); // type AND parameter name included.

So what's the difference between these two declarations? Thanks for reading and maybe answering this possibly trivial question.

-- UPDATE --

Okay, so the thing I had read was a set of style guidelines from an old professor of mine warning against including a parameter name in function definition and NOT using the parameter in the function.

void do_something(int * i) { //code that doesn't use i;} //BAD
void do_something(int *) { //code that doesn't use i;} //OK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

慢慢从新开始 2024-10-08 20:31:51

就编译器而言,没有区别。

不过,添加有意义的参数名称是一种有用的文档形式。

There is no difference, as far as the compiler is concerned.

Adding meaningful parameter names is a helpful form of documentation, though.

落墨 2024-10-08 20:31:51

不同之处在于,如果您选择一个好的参数名称,第二个版本可能更具可读性。没什么可说的了;-)

The difference is that the second version could be more readable if you choose a good parameter name. Nothing more to say ;-)

老街孤人 2024-10-08 20:31:51

这些声明之间没有技术差异。如果您有

void accumulate_stats(int * count);

类似的描述性内容,那么这将是自我文档的改进。

There is no technical difference between those declarations. If you instead had

void accumulate_stats(int * count);

or something similarly descriptive, it would be an improvement in self-documentation.

回忆凄美了谁 2024-10-08 20:31:51

在前向声明中,应该没有区别。它们都将为调用描述相同的内存布局,因此它们应该完全可以互换。

In a forward declaration, there should be no difference. They will both describe the same memory layout for the call, so they should be completely interchangeable.

浅唱々樱花落 2024-10-08 20:31:51

对于前向声明来说,这没有任何区别。

在实际定义中,不检查没有名称的参数的使用情况。这是一种拥有未使用参数的方法,无需使用 UNUSED_PARAM(x) 宏或其他一些技巧,其唯一目的是让编译器对此关闭。

For a forward declaration, it doesn't make any difference.

In the actual definition, a parameter without a name isn't checked for use. It's a way to have unused parameters without some UNUSED_PARAM(x) macro or some other trickery whose only purpose is to shut the compiler up about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文