Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 425
Athul Avatar asked Aug 30 '16 10:08

Athul


1 Answers

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.

like image 191
songyuanyao Avatar answered Oct 02 '22 10:10

songyuanyao