函数原型中的不同参数名称 [英] Different parameter name in function prototype

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

问题描述

我找到了一个在函数原型设计和声明中使用不同参数的程序,所以我制作了一个基本程序.

I found a program which uses different parameters in function prototyping and declaring, so I made a basic program.

#include <iostream>
using namespace std;

void add(int a, int b);

int main()
{
     add(3,4);
}

void add(int c, int d){
    int e = c + d;
    cout << e << endl;
}

我运行了该程序,它可以工作.这是否意味着在函数原型"和函数声明"中都不必使用相同的参数名称?

I run this program and it works. Does that mean it isn't necessary to same parameter name in both "function prototyping" and in "function declaring"?

推荐答案

是的,声明和定义中使用的参数名称不必相同.相反,参数的类型(和顺序)应该相同.实际上,参数名称不是必需的,尤其是在函数声明中,即使在定义中,如果您不使用它们,也可以将其省略.

Yes, the name of parameters used in declaration and definition doesn't have to be the same. Instead, the type of parameters (and order), should be the same. In fact, parameter names are not necessary especially in function declaration, even in definition they also could be omitted if you don't use them.

[dcl.fct]/13 :

(重点是我的)

可以选择提供标识符作为参数名称; (如果存在于函数定义中)( [dcl.fct.def] ) ,它命名一个参数. [注意:特别是,参数名称在函数定义中也是可选的,并且在不同声明中用于参数的名称与函数的定义不必相同.如果参数名称出现在不是定义的函数声明中,则不能在其函数声明符之外使用它,因为这是其潜在范围的范围([

An identifier can optionally be provided as a parameter name; if present in a function definition ([dcl.fct.def]), it names a parameter. [ Note: In particular, parameter names are also optional in function definitions and names used for a parameter in different declarations and the definition of a function need not be the same. If a parameter name is present in a function declaration that is not a definition, it cannot be used outside of its function declarator because that is the extent of its potential scope ([basic.scope.proto]). — end note ]

[dcl.fct]/8 :

返回类型,参数类型列表 cv-qualifier-seq ,以及该函数是否具有非抛出的

The return type, the parameter-type-list, the ref-qualifier, the cv-qualifier-seq, and whether the function has a non-throwing exception-specification, but not the default arguments ([dcl.fct.default]) or the exception specification ([except.spec]), are part of the function type.

请注意,不包括参数名称的参数类型列表是函数类型的一部分.

Note that the parameter-type-list, not including their names, is part of the function type.

这篇关于函数原型中的不同参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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