13

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?

8
  • 2
    I think the readability would be much better if parameter name is included in the prototype.
    – Thomson
    Mar 8, 2011 at 15:04
  • How exactly does it add to readability?
    – Lockhead
    Mar 8, 2011 at 15:05
  • 1
    in your case you have two int parameters, without the names how does one know whether to pass in x first or y?
    – Nim
    Mar 8, 2011 at 15:12
  • In functions that have obscure parameter names(like x and y), how does it do any difference to write them in the prototype?
    – Lockhead
    Mar 8, 2011 at 15:17
  • The order of the parameters in your add function are not important, and most people already know that a function named "add" would add its parameters together without applying meaning to the values, so the names aren't really important in this example. But consider a function like strstr. The order is important. Which parameter comes first? Wouldn't it be easier to remember if the parameters had distinctive names, such as needle and haystack, and if your editor would show those names to you while you wrote your code or when it reported errors? That's readability. Mar 8, 2011 at 15:27

6 Answers 6

23

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.
8

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

5

You don't need parameter names in declarations. They are purely documentation.

You don't even need names in definitions:

int f(int)
{
    return 0;
}

compiles just fine in C++ (though not in C). This is sometimes useful for e.g. inheritance, overloading, function pointers.

7
  • 2
    You do need the names in the definition if the definition uses that parameter. Mar 8, 2011 at 15:06
  • 1
    You can't access it. You might put it in in at least two situations: (1) for forwards compatibility, in case you want to start using it later without having to update all call sites; (2) when you override a virtual method, but don't want to inspect the argument.
    – Thomas
    Mar 8, 2011 at 15:08
  • 2
    Omitting the name of an unused parameter in the definition also often has the effect of suppressing any "unused parameter" warnings from the compiler. Mar 8, 2011 at 15:11
  • 1
    @MisterSir: There are situations when you have a parameter which you don't actually use. It is used for overload resolution only.For example, see std::advance function. If you name the parameter, you'll get a warning Mar 8, 2011 at 15:19
  • 2
    @MisterSir on some occasions you don't want to access it, in particular our old friend iterator::operator++(int) where the int is only there to indicate that it is a post-increment operator.
    – CashCow
    Mar 8, 2011 at 15:23
3

You do not need to include parameter names in function prototypes. You only need the complete signature, which includes the types of parameters and (in the case of function template specializations) return values.

However, it is still common to include parameter names in order to write self-documenting code. That is, a function with this declaration:

void foo(int number_of_foos_desired, string foo_replacement);

is easier to understand by looking only at the prototype, perhaps, than this one:

void foo(int, string);

Many modern IDEs will also pop up the parameter names as you type when you write code that calls this function. They may not be able to pop up this information if you dont include the parameter names in the prototype.

2
  • I always thought a function signature doesn't include its return type(unless it's a template specialization)
    – Lockhead
    Mar 8, 2011 at 15:09
  • @Mister: Generally this is true. But int he case of specializations of function teplates, the return type is part of the signature. Mar 8, 2011 at 15:13
1

It has alot to do with the signature of the function.

One of the benefits of using .h files is that when someone comes along and wants to get a sense of what your program/api does, they can look at your header file and get a sense of what operations are being carried out, their inputs and outputs, how everything is going together, etc.

If you were to come across a method like

int doStuff(int,int)

this would be alot less telling than a method with a signature of say:

int doStuff(int firstNumberToAdd, int secondNumberToAdd);

with the second, you at least get some idea of the operations that are being carried out, and what is happening. This is the idea behind writing self documenting code.

If your interested, you may check out Code Complete by Steve McConnell.

1
  • The parameter names are not part of the signature. Mar 8, 2011 at 15:28
1

There are different scenarios to it. I found it very helpful in dealing with inheritance and virtual functions. If you're using a virtual function that generates unused warning in sub class, you can omit the variable names.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.