Reputation: 177
As I understand it, in a function declaration there is no need to give variable names, but it is still recommended.
For example: the setcur() function accepts two parameters : row number and and column number.Hence can be declared as follows:
void setcur(int, int);
void setcur(int row, int col);
Why is it recommended to give variable names in a function declaration?
Upvotes: 3
Views: 2399
Reputation: 56863
While the names are not technically required, they serve as documentation to help the user. Also, they help your IDE to show better signatures, completions, etc.
Adding documentation in some tool-aware form might look like an alternative, but your code might also be used with different IDEs and, unlike comments, the variable names in the declarations are the only standardized way of providing more semantic information in the declaration of a function.
Also note that in case of libraries, the user of the library might not even see the definition of the function, all he has is the header file with the declaration and a binary library to link against.
Upvotes: 0
Reputation: 804
There's been significant discussion here if you're really curious:
Why do function prototypes include parameter names when they're not required?
The short answer is readability, but to use your example, basically
void setcur(int,int);
can be implemented as
(1) void setcur(int row,int col);
or
(2) void setcur(int col,int row);
(any other implementations are possible, but allow 2 as an example).
With the variable names in the header, you can easily get a sense of what to pass for row and col. Otherwise you need to dig into the code file which may exist separate from the header. Imagine if all you had was
void setcur(int, int)
and assumed (1), but it really was (2). You would have a hard to debug failure in your code.
Upvotes: 1
Reputation: 56557
Adding a decent variable name improves readability and helps documenting your function. Consider e.g.
void area(int, int); // which parameter comes first? area of what?
vs
void area(int radius, int height); // now it's clear that it is a cylinder, order is also clear
You may also want to use e.g. doxygen to generate the documentation automatically, in which case you usually document the header files. In that case, it makes sense to name the function parameters, so they correspond to the names of the doxygen's \param
s.
Upvotes: 1
Reputation: 850
It really is more for readability sake. You only really need the variable type in the function declaration; however, it is good for someone reading your code to understand what these inputs actually are, assuming your name them something appropriate.
It will make your life a lot easier if you are working on a large file and you don't remember what your function in your .h file takes in as input.
Upvotes: 5