Function Definitions

A function definition contains a function declaration and the body of a function.

C The syntax for a C function definition is as follows:

   .--------------------.
   V                    |
>>---+-type_specifier-+-+--function_name------------------------>
     +-extern---------+
     '-static---------'
 
>--(--+---------------------------------------+--)-------------->
      | .-,---------------------.             |
      | V                       |             |
      '---parameter_declaration-+--+--------+-'
                                   '-,--...-'
 
>--| function_body |-------------------------------------------><
 
function_body:
 
|--block_statement----------------------------------------------|
 
 

C++The syntax for a C++ function definition is as follows:

   .--------------------.
   V                    |
>>---+-type_specifier-+-+--function_name------------------------>
     +-extern---------+
     '-static---------'
 
>--(--+---------------------------------------+--)-------------->
      | .-,---------------------.             |
      | V                       |             |
      +---parameter_declaration-+--+--------+-+
      |                            '-,--...-' |
      '-...-----------------------------------'
 
>--+----------+--+-------------------------+-------------------->
   +-const----+  '-exception_specification-'
   '-volatile-'
 
>--| function_body |-------------------------------------------><
 
function_body:
 
|--+-| body |--------------------------+------------------------|
   '-try--| body |--| catch_handlers |-'
 
body:
 
|--+---------------------------------+--block_statement---------|
   '-:--constructor_initializer_list-'
 
catch_handlers:
 
   .---------------------------------------------------------.
   V                                                         |
|----catch--(--+-parameter_declaration-+--)--block_statement-+--|
               '-...-------------------'
 
 

In both languages, a function definition contains the following:

C++A C++ function definition may optionally contain the following:

A function can be called by itself or by other functions. By default, function definitions have external linkage, and can be called by functions defined in other files. A storage class specifier of static means that the function name has global scope only, and can be directly invoked only from within the same translation unit.

C++This use of static is deprecated in C++. Instead, place the function in the unnamed namespace.

C In C, if a function definition has external linkage and a return type of int, calls to the function can be made before it is visible because an implicit declaration of extern int func(); is assumed. To be compatible with C++, all functions must be declared with prototypes.

C If the function does not return a value, use the keyword void as the type specifier. If the function does not take any parameters, use the keyword void rather than an empty parameter list to indicate that the function is not passed any arguments. In C, a function with an empty parameter list signifies a function that takes an unknown number of parameters; in C++, it means it takes no parameters.

C In C, you cannot declare a function as a struct or union member.

Compatibility of Function Declarations

All declarations for a given function must be compatible; that is, the return type is the same and the parameters have the same type.

Compatibility of Function Types

C The notion of type compatibility pertains only to C. For two function types to be compatible, the return types must be compatible. If both function types are specified without prototypes, this is the only requirement.

For two functions declared with prototypes, the composite type must meet the following additional requirements:

and may use the [*] notation in their sequences of declarator specifiers to specify variable length array types.

If the function declarator is not part of the function definition, the parameters may have incomplete type. The parameters may also specify variable length array types by using the [*] notation in their sequences of declarator specifiers. The following are examples of compatible function prototype declarators:

double maximum(int n, int m, double a[n][m]);
double maximum(int n, int m, double a[*][*]);
double maximum(int n, int m, double a[ ][*]);
double maximum(int n, int m, double a[ ][m]);

Examples of Function Definitions

The following example is a definition of the function sum:

int sum(int x,int y)
{
   return(x + y);
}

The function sum has external linkage, returns an object that has type int, and has two parameters of type int declared as x and y. The function body contains a single statement that returns the sum of x and y.

In the following example, ary is an array of two function pointers. Type casting is performed to the values assigned to ary for compatibility:

#include <stdio.h>
 
typedef void (*ARYTYPE)();
 
int func1(void);
void func2(double a);
 
int main(void)
{
  double num = 333.3333;
  int retnum;
  ARYTYPE ary[2];
  ary[0]=(ARYTYPE)func1;
  ary[1]=(ARYTYPE)func2;
 
  retnum=((int (*)())ary[0])();      /*  calls func1  */
  printf("number returned = %i\n", retnum);
  ((void (*)(double))ary[1])(num);   /*  calls func2  */
 
  return(0);
}
 
int func1(void)
{
  int number=3;
  return number;
}
 
void func2(double a)
{
  printf("result of func2 = %f\n", a);
}

The following is the output of the above example:

number
returned = 3
result of func2 = 333.333300

Related References

IBM Copyright 2003