Is there a way to get warned about missmatching of the argument names between a function declaration and its definition?
Declaration
double divide(int a, int b);
Definition
double divide(int b, int a)
{
return a / b;
}
For a user, that use the function divide
, will expect a / b as a result and not b / a.
I know, the compiler can't do that, but are there some static analysis tools that can do that? If yes, which ones?
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
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.
Declaration means that variable is only declared and memory is allocated, but no value is set. However, definition means the variables has been initialized. The same works for variables, arrays, collections, etc.
The caller's arguments passed to the function's parameters do not have to have the same names.
You can use clang-tidy. Calling it a compiler is a bit of a stretch, but maybe there is an option to make clang emit clang-tidy warnings. The specific option you want is readability-inconsistent-declaration-parameter-name
.
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