简体   繁体   English

函数原型中的不同参数名称

[英]Different parameter name in function prototype

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]/13

(emphasis mine) (强调我的)

An identifier can optionally be provided as a parameter name;可以选择将标识符作为参数名称提供; if present in a function definition ( [dcl.fct.def] ), it names a parameter.如果出现在函数定义 ( [dcl.fct.def] ) 中,它会命名一个参数。 [ 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 ]).如果参数名称出现在不是定义的函数声明中,则不能在其函数声明之外使用,因为这是其潜在作用域 ([ basic.scope.proto ]) 的范围。 — end note ] — 尾注 ]

And [dcl.fct]/8 :[dcl.fct]/8

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.返回类型、参数类型列表引用限定符cv-qualifier-seq以及函数是否具有非抛出 异常规范,但没有默认参数 ( [dcl.fct.default] )或异常规范 ( [except.spec] ) 是函数类型的一部分。

Note that the parameter-type-list, not including their names, is part of the function type.请注意,参数类型列表(不包括它们的名称)是函数类型的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM