C ++ function prototypes and variable names for data types only

When declaring a function prototype in C ++, there is a difference between the following:

void SomeFunction( int Argument ) { //Stuff } 

Vs

 void SomeFunction( int ) { //Stuff } 

Essentially, what I'm asking is, why are you writing the name of the variable argument in the function prototype, and not just the data type?

+4
source share
4 answers

Argument names are not needed for the compiler in function declarations. This is for human consumption. They provide additional information on what the function does. Good function names combined with good argument names serve as instant documentation for your method.

+4
source

You need an argument name if you are really going to use an argument. Some compilers (e.g. Microsoft VC ++) will give you a warning if you call an argument but don't use it anywhere in a function.

PS What you used in your example is not a prototype, but the actual definition of a function. In the prototype, the argument name is optional.

+2
source

additional commentary on the difference between “announcement” and “definition”. Both of you are definitions:

 void SomeFunction( int Argument ) { //Stuff } 

A prototype would be a declaration and would look like this:

 void SomeFunction( int ) ; 

So, you can have a declaration in your heading, as indicated above. Then in your cpp you define the function as follows:

 void SomeFunction( int Argument ) { Argument = Argument + 1; } 

As you can see, the declaration does not indicate the name of the argument, but the definition then indicates it and uses it.

+2
source

You do not need to write the name of the argument in the definition or in the declaration. If so, they should not even be the same. You must write one if you plan to actually use the argument.

+1
source

All Articles