简体   繁体   中英

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 :

(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. [ 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 ]

And [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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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