Skip to the content of the web site.

Lesson 222: Function declarations without parameter identifiers?

Previous lesson Next lesson


Up to this point, we have always used parameter identifiers when we declared a function or member function:

unsigned int factorial( unsigned int n );
double fast_sin( double x );
double fast_cos( double x );

You may find examples of C++ code where the parameter identifiers are not necessary, and this is acceptable:

unsigned int factorial( unsigned int );
double fast_sin( double );
double fast_cos( double );

The only purpose of the function declaration is to inform the compiler that, in this case, factorial, fast_sin and fast_cos are functions, the types they return, and the types of the arguments. There is no real reason to provide the identifiers, as the types provide all the information required by the compiler. The issue is, however, that the identifiers can provide information to the reader. For example, the

bool Weighted_graph::is_connected( size_t start_vertex, size_t end_vertex, bool recursing = false );
bool update( double x, size_t n, unsigned int flags );

In all three cases, the identifiers give some clue as to the purpose of the parameters: in the first example, the reader can immediately determine that the first two variables are vertices within a graph and that the Boolean parameter indicates whether or not the current call is a recursive call, while in the second, the third parameter are flags for the function.

Problems

One serious issue with parameter identifiers is that nothing in the language specifications requires that the identifiers are the same, and this can lead to issues if they are not synchronized:

// Function definition
double shortest_path( bool is_connected );

// Function definition
double shortest_path( bool is_not_connected ) {
	// some code here...

	return 0.0;
}

In this case, the parameter identifier is_connected suggests that this parameter is true if the graph is connected, and false otherwise; however, in the actual function definition, the parameter value appears to be the opposite. Thus, a user looking at the parameter identifier in the function definition may inadvertently pass the wrong value.

Critical warning:

While it is possible to declare functions without parameter identifiers, the additional information that the identifiers give to the reader more than compensates for the additional effort of ensuring that the parameter identifiers are kept synchronized.


Previous lesson Next lesson