ILE C/C++ Language Reference


Function Declarations

A function declaration establishes the name of the function and the number and types of its parameters. A function declaration consists of a return type, a name, and a parameter list. In addition, a function declaration may optionally specify the function's linkage. In C++, the declaration can also specify an exception specification, a const-qualification, or a volatile-qualification.

A declaration informs the compiler of the format and existence of a function prior to its use. A function can be declared several times in a program, provided that all the declarations agree. Every function must be declared implicitly or explicitly before it can be called. In C89, if a function is called without an explicit prototype, the compiler provides an implicit declaration. The compiler checks for mismatches between the parameters of a function call and those in the function declaration. The compiler also uses the declaration for argument type checking and argument conversions.

A function definition contains a function declaration and the body of the function. A function can only have one definition.

Declarations are typically placed in header files, while function definitions appear in source files.

>>-+--------+--+----------------+--function_name---------------->
   +-extern-+  '-type_specifier-'
   '-static-'
 
      .-,-------------.
      V               |
>--(----+-----------+-+--+--------+--)--+----------+------------>
        '-parameter-'    '-,--...-'     +-const----+
                                        '-volatile-'
 
>--+-------------------------+--;------------------------------><
   '-exception_specification-'
 
 

A function argument is an expression that you use within the parentheses of a function call. A function parameter is an object or reference declared within the parenthesis of a function declaration or definition. When you call a function, the arguments are evaluated, and each parameter is initialized with the value of the corresponding argument. The semantics of argument passing are identical to those of assignment.

Some declarations do not name the parameters within the parameter lists; the declarations simply specify the types of parameters and the return values. This is called prototyping A function prototype consists of the function return type, the name of the function, and the parameter list. The following example demonstrates this:

int func(int,long);

Function prototypes are required for compatibility between C and C++. The non-prototype form of a function that has an empty parameter list means that the function takes an unknown number of parameters in C, whereas in C++, it means that it takes no parameters.

Function Return Type

You can define a function to return any type of value, except arrays and the result of a function call. These exclusions must be handled by returning a pointer to the array or function. A function may return a pointer to function, or a pointer to the first element of an array, but it may not return a value that has a type of array or function. To indicate that the function does not return a value, declare it with a return type of void.

A function cannot be declared as returning a data object having a volatile or const type, but it can return a pointer to a volatile or const object.

C++Limitations When Declaring Functions in C++

Every function declaration must specify a return type.

OS/400 The ILE C++ compiler supports implicit int as a function return type, provided that LANGLVL(*EXTENDED) or LANGLVL(*LEGACY) has been specified.

Only member functions may have const or volatile specifiers after the parenthesized parameter list.

The exception_specification limits the function from throwing only a specified list of exceptions.

Other Limitations When Declaring a Function

The ellipsis (...) may be the only argument in C++. In this case, the comma is not required. In C, you cannot have an ellipsis as the only argument.

Types cannot be defined in return or argument types. For example, the C++ compiler will allow the following declaration of print():

struct X { int i; };
void print(X x);
 

The C compiler will allow the following declaration:

struct X { int i; };
void print(struct X x);

Neither the C nor C++ compiler will not allow the following declaration of the same function:

void print(struct X { int i; } x);   //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 another example, the C++ compiler will allow the following declaration of counter():

enum count {one, two, three};
count counter();

Similarly, the C compiler will allow the following declaration:

enum count {one, two, three};
enum count counter();

Neither compiler will not allow the following declaration of the same function:

enum count{one, two, three} counter();   //error

In the attempt to declare counter(), the enumeration type definition cannot appear in the return type of the function declaration.

Related References


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