ILE C/C++ Language Reference

C++ Function Declarations

C++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.

Related References

Multiple Function Declarations

C++All function declarations for one particular function must have the same number and type of parameters, and must have the same return type.

These return and parameter types are part of the function type, although the default arguments and exception specifications are not.

If a previous declaration of an object or function is visible in an enclosing scope, the identifier has the same linkage as the first declaration. However, a variable or function that has no linkage and later declared with a linkage specifier will have the linkage you have specified.

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 parameter 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 type qualifier is also part of the function type, but can only be part of a declaration or definition of a nonstatic member function.

If two function declarations match in both return type and parameter lists, then the second declaration is treated as redclaration of the first. The following example declares the same function:

int foo(const string &bar);
int foo(const string &);

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

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

Related References

Parameter Names in Function Declarations

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

  1. If two parameter names have the same name within a single declaration. This is an error.
  2. If a parameter 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 parameter declaration.

In the following example, the third parameter name intersects is meant to have enumeration type subway_line, but this name is hidden by the name of the first parameter. The declaration of the function subway() causes a compile-time error because subway_line is not a valid type name because the first parameter name subway_line hides the namespace scope enum type and cannot be used again in the second parameter.

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

Related References


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]