C++ Function Declarations

In C++, you can specify the qualifiers volatile and const in member function declarations. You can also specify exception specifications in function declarations. All C++ functions must be declared before they can be called.

Types cannot be defined in return or argument types. For example, the following declarations are not valid in C++:

void print(struct X { int i; } x);           //error
enum count{one, two, three} counter();       //error

This example attempts to declare a function print() that takes an object x of class X as its argument. However, the class definition is not allowed within the argument list. In the attempt to declare counter(), the enumeration type definition cannot appear in the return type of the function declaration. The two function declarations and their corresponding type definitions can be rewritten as follows:

struct X { int i; };
void print(X x);
enum count {one, two, three};
count counter();


Multiple Function Declarations

All function declarations for a particular function must have the same number and type of arguments, and must have the same return type and the same linkage keywords. These return and argument types are part of the function type, although the default arguments are not.

For the purposes of argument matching, ellipsis and linkage keywords are considered a part of the function type. They must be used consistently in all declarations of a function. If the only difference between the argument types in two declarations is in the use of typedef names or unspecified argument array bounds, the declarations are the same. A const or volatile specifier is also part of the function type, but can only be part of a declaration or definition of a nonstatic member function.

Declaring two functions differing only in return type is not valid function overloading, and is flagged as an error. For example:

void f();
int f();      // error, two definitions differ only in
              // return type
int g()
{
   return f();
}


Checking Function Calls

The compiler checks C++ function calls by comparing the number and type of the actual arguments used in the function call with the number and type of the formal arguments in the function declaration. Implicit type conversion is performed when necessary.

Argument Names in Function Declarations

You can supply argument names in a function declaration, but the compiler ignores them except in the following two situations:

  1. If two argument names have the same name within a single declaration. This is an error.
  2. If an argument name is the same as a name outside the function. In this case the name outside the function is hidden and cannot be used in the argument declaration.

In the following example, the third argument intersects is meant to have enumeration type subway_line, but this name is hidden by the name of the first argument. The declaration of the function subway() causes a compile-time error because subway_line is not a valid type name in the context of the argument declarations.

enum subway_line {yonge, university, spadina, bloor};
int subway(char * subway_line, int stations,
                  subway_line intersects);