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. Implicit declaration of functions is not allowed: every function must be explicitly declared 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 nonprototype 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 an array type or a function type. 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.

The return type of a function must be void if the return statement does not contain an expression. However, if an expression does appear in the return statement, then the return type of the function cannot be void: the compiler converts the return expression as if by assignment to the return type of the function.

 The return statement of a function does not require an expression if the function has the return type void, or is a constructor or destructor. In these cases, the function does not return a value. When a function returns a value, the return statement must contain an expression, which is returned to the caller of the function after being implicitly converted to the return type of the function in which it appears.

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.

Limitations When Declaring Functions in C++

Every function declaration must specify a return type.

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 allows the following declaration of print():

struct X { int i; };

void print(X x);
The C compiler allows the following declaration:

struct X { int i; };

void print(struct X x);

Neither the C nor C++ compiler allows 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 allows the following declaration:

enum count {one, two, three};

enum count counter();

Neither compiler allows 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.


Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates