Why Does C++ Code Missing a Formal Argument Name in a Function Definition Compile Without Warnings

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

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.

default parameters without name in c ++

In function declaration/definition, a parameter may have or have not a name, this also applies to a parameter with default value.

But to use a parameter inside a function, a name must be provided.

Normally when declare a function with default parameter

// Unnamed default parameter. 
void foo1(int = 3);

In function definition

void foo1(int a)
{
std::cout << a << std::endl;
}

Then you can call

foo1();   // the same as call foo1(3)
foo1(2);

parameter name omitted, C++ vs C

No, in C you cannot omit identifiers for parameters in function definitions.

The C99 standard says:

[6.9.1.5] If the declarator includes a parameter type list, the
declaration of each parameter shall include an identifier, except for
the special case of a parameter list consisting of a single parameter
of type void, in which case there shall not be an identifier. No
declaration list shall follow.

The C++14 standard says:

[8.3.5.11] An identifier can optionally be provided as a parameter
name; if present in a function definition , it names a parameter
(sometimes called “formal argument”). [Note: In particular, parameter
names are also optional in function definitions
and names used for a
parameter in different declarations and the definition of a function
need not be the same.]

C++ functions argument datatypes have no names

It is valid because the language specification says so. The names aren't used anywhere, so they aren't needed.

Why can I call a function from another file (with warning) but cannot use a variable from another file without declaring it?

First, the "implicit int" rule is long gone (since C99) and there's no case for not providing declarations in modern C (i.e. C99 and later).


However, why functions are OK with implicit declarations but not variables is because that's how it was initially defined in the pre-standard C (impplicit rules were valid in C89, too).

From Dennis Ritchie's C reference manual:


  1. Implicit declarations

It is not always necessary to specify both the storage class and the type of identifiers in a declaration.
Sometimes the storage class is supplied by the context: in external
definitions, and in declarations of formal parameters and structure
members. In a declaration inside a function, if a storage class but no
type is given, the identifier is assumed to be int; if a type but no
storage class is indicated, the identifier is assumed to be auto. An
exception to the latter rule is made for functions, since auto
functions are meaningless (C being incapable of compiling code into
the stack). If the type of an identifier is ‘‘function returning
...’’, it is implicitly declared to be extern. In an expression, an
identifier followed by ( and not currently declared is contextually
declared to be ‘‘function returning int’’.

Undefined identifiers not followed by ( are assumed to be labels which will be defined later in the function. (Since a label is not an
lvalue, this accounts for the ‘‘Lvalue required’’ error message
sometimes noticed when an undeclared identifier is used.) Naturally,
appearance of an identifier as a label declares it as such.

(emphasis mine)

That means the following are OK:

// assumed to return int
func()
{
return 0;
}

// Type of 'some_var' defaults to int
void func2(some_var)
{
}

But this is not OK:

int func()
{
// Assumed to be labels; it's undeclared variable.
i;
}

Why does a function with no parameters (compared to the actual function definition) compile?

All the other answers are correct, but just for completion

A function is declared in the following manner:

  return-type function-name(parameter-list,...) { body... }

return-type is the variable type that the function returns. This can not be an array type or a function type. If not given, then int
is assumed
.

function-name is the name of the function.

parameter-list is the list of parameters that the function takes separated by commas. If no parameters are given, then the function
does not take any and should be defined with an empty set of
parenthesis or with the keyword void. If no variable type is in front
of a variable in the paramater list, then int is assumed
. Arrays and
functions are not passed to functions, but are automatically converted
to pointers. If the list is terminated with an ellipsis (,...), then
there is no set number of parameters. Note: the header stdarg.h can be
used to access arguments when using an ellipsis.

And again for the sake of completeness. From C11 specification 6:11:6 (page: 179)

The use of function declarators with empty parentheses (not
prototype-format parameter type declarators) is an obsolescent
feature
.

C Function is deprecated

Function is a typedef (an alias of a pointer to function returning int) marked as deprecated by the library:

typedef int Function () __attribute__ ((deprecated));

Just use:

typedef struct {
char *name; /* User printable name of the function. */
int (*func)(); /* Function to call to do the job. */
char *doc; /* Documentation for this function. */
} COMMAND;

Avoid warning 'Unreferenced Formal Parameter'

In C++ you don't have to give a parameter that you aren't using a name so you can just do this:

void Child::Function(int)
{
//Do nothing
}

You may wish to keep the parameter name in the declaration in the header file by way of documentation, though. The empty statement (;) is also unnecessary.



Related Topics



Leave a reply



Submit