Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ code missing a formal argument name in a function definition compile without warnings?

While getting started with some VS2005-generated MFC code, I noticed it overrode a method with something like this:

void OnDraw(CDC* /*pDC*/) {     ...     // TODO: Add your code here } 

So of course, as soon as I added something I realized I needed to un-comment the pDC formal argument in order to compile, but I'm confused as to how/why a C++ function can compile (with no warnings) when the formal argument only has a type and not a name:

void foo(int) {     int x = 3; } int main() {     foo(5);     return 0; } 

Shouldn't this generate at least a warning (with -Wall or /W4)? It doesn't seem to. Am I missing something? Is there a case where this is useful or is it just because the compiler can't tell the difference between a function declaration (only types required) and a definition (fully specified) until after the line has been processed?

like image 341
Andrew Coleson Avatar asked Feb 23 '10 16:02

Andrew Coleson


People also ask

How do you call a function with no arguments in C?

If a function takes no parameters, the parameters may be left empty. The compiler will not perform any type checking on function calls in this case. A better approach is to include the keyword "void" within the parentheses, to explicitly state that the function takes no parameters.

What does it mean for a function to have arguments coding?

Argument definition. An argument is a way for you to provide more information to a function. The function can then use that information as it runs, like a variable. Said differently, when you create a function, you can pass in data in the form of an argument, also called a parameter.

What happens if function is not declared in C?

In C, if a function is called before its declaration, the compiler assumes the return type of the function as int. For example, the following program fails in the compilation.

What are the arguments in this function call?

Arguments are the values passed from a function call (i.e., they are the values appearing inside the parentheses of the call) and are sent into the function). The following example is based on pass-by-value, the most common and familiar argument passing technique.


1 Answers

Because sometimes you have a parameter that's required by an interface but the function doesn't use it. Maybe the parameter is no longer necessary, is only necessary in other functions that must use the same signature (especially so they can be called through pointers) or the functionality hasn't been implemented yet. Having parameters that aren't used can be particularly common in generated or framework code for this reason (and that's probably why the MFC generated code has the name commented out).

As to why there's no warning - I guess it's because whether this is a problem is a subjective thing and other people (particularly compiler implementers) don't see it as a problem. Once you actually go to use the parameter, you'll get the compiler to complain if you forget to uncomment the name so you get the compiler complaining only when you really need it to (the compiler's version of the agile YAGNI: "You Aren’t Gonna Neet It" philosophy).

The opposite does seem to generally occur when you crank up warnings - named parameters that aren't used generate warnings - again that's probably why the generated function has the name commented out.

like image 50
Michael Burr Avatar answered Oct 19 '22 17:10

Michael Burr