Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do function prototypes include parameter names when they're not required?

I always thought that a function prototype must contain the parameters of the function and their names. However, I just tried this out:

int add(int,int);

int main()
{
    std::cout << add(3,1) << std::endl;
}

int add(int x, int y)
{
    return x + y;
}

And it worked! I even tried compiling with extreme over-caution:

g++ -W -Wall -Werror -pedantic test.cpp

And it still worked. So my question is, if you don't need parameter names in function prototypes, why is it so common to do so? Is there any purpose to this? Does it have something to do with the signature of the function?

like image 343
Lockhead Avatar asked Mar 08 '11 15:03

Lockhead


People also ask

Do function prototypes require parameter names?

In a prototype, parameter names are optional (and in C/C++ have function prototype scope, meaning their scope ends at the end of the prototype), however, the type is necessary along with all modifiers (e.g. if it is a pointer or a reference to const parameter) except const alone.

Is it necessary to include variables in the function prototype?

Examples: int add( int a, int b ); int add( int, int ); Note that the function prototypes end with semicolons, indicating that this is not a function, but merely a prototype of a function to be provided elsewhere. Note also that the variable names are not required in the function prototype.

Why do we need parameters in functions?

Parameters allow us to pass information or instructions into functions and procedures . They are useful for numerical information such as stating the size of an object. Parameters are the names of the information that we want to use in a function or procedure.

What is the purpose of a function prototype?

A function prototype is a definition that is used to perform type checking on function calls when the EGL system code does not have access to the function itself. A function prototype begins with the keyword function, then lists the function name, its parameters (if any), and return value (if any).


2 Answers

No, these are not necessary, and are mostly ignored by the compiler. You can even give them different names in different declarations; the following is entirely legal:

int foo(int bar);
int foo(int biz);
int foo(int qux) {
    ...
}

(The compiler does check that each name is used only once in the same argument list: int foo(int bar, int bar); is rejected.)

The reason to put them in is documentation:

  • If someone reads your header file, they can tell at a glance what each parameter is used for.
  • If you use a fancy IDE, it can show you the parameter names when you begin typing the function call.
  • Documentation tools like Doxygen can parse the parameter names and show them in the documentation.
like image 144
Thomas Avatar answered Nov 15 '22 16:11

Thomas


Parameter names are completely optional, and have no effect on compilation. They may be placed there for better readability of code.

like image 32
Christopher Armstrong Avatar answered Nov 15 '22 18:11

Christopher Armstrong