Function argument names in declarations (that most likely reside in the header file) are seemingly completely ignored by the compiler. What are the reasons for allowing the following to compile using either declaration version 1 or 2?
implementation
void A::doStuff(int numElements, float* data)
{
//stuff
}
declaration - Version 1
class A
{
public:
void doStuff(int numElements, float* data);
}
declaration - Version 2
class A
{
public:
void doStuff(int, float*);
}
It is an error if the number of arguments in a function definition, declaration, or call does not match the prototype. If the data type of an argument in a function call does not match the corresponding type in the function prototype, the compiler tries to perform conversions.
To call a function which takes no arguments, use an empty pair of parentheses. Example: total = add( 5, 3 );
Yes, it matters. The arguments must be given in the order the function expects them.
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.
The compiler only needs to know what kind of arguments the method requires. It's unimportant for the compiler how you call them.
The compiler needs to know the argument types for several reasons:
However, I suggest to use the first header version. It helps other developers (and yourself) to use the functions and know what parameters have which meaning.
Parameter names aren't part of the function signature. Unless you use them, you don't need to have names even in the function implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With