@MarceloCantos: no, C does not require them; even better, the C standard still even allows you to leave an empty parameters list, which means that no information about the parameters is given (C99, §6.7.5.3, ¶14); contrast this with C++, where it means that the function accepts no arguments.
–
Matteo ItaliaNov 17 '11 at 22:11

1

@WTP: that was an ironical "better" :) Even the standard says (§6.11.6 ¶1) that "The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.", since it quite defeats part of the purpose of prototypes.
–
Matteo ItaliaNov 17 '11 at 22:16

@MatteoItalia: I just realised that I'm confusing declarations and definitions. A definition requires parameter names in C — and not in C++ — but the declaration above is valid in either.
–
Marcelo CantosNov 17 '11 at 22:17

3 Answers
3

Yes, it's considered good practice to name the arguments even in the prototypes.

You will usually have all your prototypes in the header file, and the header may be the only thing your users ever get to inspect. So having meaningful argument names is the first level of documentation for your API.

Likewise, comments about the what the functions do (not how they're implemented, of course) should go in the header, together with their prototypes.

A well-written header file may be the most important part of your library!

As a curious aside, constness of arguments is an implementation detail. So if you don't mutate an argument variable in your implementation, only put the const in the implementation:

/* Header file */
/* Computes a thingamajig with given base
* in the given number of steps.
* Returns half the thingamajig, or -1 on error.
*/
int super_compute(int base, int steps);

Why would you want to deny knowledge of constness to the the client that only sees the header file?
–
David HeffernanNov 17 '11 at 22:02

2

@DavidHeffernan: The arguments are passed by value, so there's nothing the user could possibly do with this information.
–
Kerrek SBNov 17 '11 at 22:02

1

@WTP: Could you explain? I'm not familiar with autocomplete.
–
Kerrek SBNov 17 '11 at 22:03

4

@Kerrek SB autocomplete means that when you type in part of a function or variable name, your editor can automatically expand it to the full symbol. This is similar to Google's autocomplete feature. The advantage of having argument names in prototypes here is that the editor can insert placeholders you can tab through, and those placeholders will be meaningful to the programmer. Just having "int" or "char *" doesn't describe what the argument is about. This is especially useful if a function has many arguments and you don't want to look up the documentation every time.
–
user142019Nov 17 '11 at 22:06

3

[I we ultimately concluded that this was indeed perfectly OK, both in C and in C++.]
–
Kerrek SBNov 18 '11 at 1:20

Some IDEs and editors will pull prototype information out of header files and provide the parameter information as hints while typing. If the names are available, that helps write code faster (and can help avoid some bugs).

I definitely recommend including the names of the parameters. If you're writing a library, it is certainly useful for those who will use your library to be able to glean what a function does from its prototype in your header files. Consider memcpy for instance. Without the names of the parameters, you'd be lost to know which is the source and which is the target. Finally, it is easier to include the names than to remove them when you copy your function definition to make it into a prototype. If you keep the names, you only need to add a semicolon at the end.