HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 3 Data Types and Declarations

Function Definitions

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

A function definition introduces a new function.

Syntax

function-definition ::=
[declaration-specifiers] declarator [declaration-list] compound-statement

Description

A function definition provides the following information about the function:

  1. Type.

    You can specify the return type of the function. If no type is provided, the default return type is int. If the function does not return a value, it can be defined as having a return type of void. You can declare functions as returning any type except a function or an array. You can, however, define functions that return pointers to functions or pointers to arrays.

  2. Formal parameters. There are two ways of specifying the type and number of the formal parameters to the function:

    1. A function declarator containing an identifier list.

      The identifiers are formal parameters to the function. You must include at least one declarator for each declaration in the declaration list of the function. These declarators declare only identifiers from the identifier list of parameters. If a parameter in the identifier list has no matching declaration in the declaration list, the type of the parameter defaults to int.

    2. A function declarator containing a parameter type list (prototype form).

      In this case, the function definition cannot include a declaration list. You must include an identifier in each parameter declaration (not an abstract declarator). The one exception is when the parameter list consists of a single parameter of type void; in this case do not use an identifier.

      NOTE: Function prototypes can be used only in ANSI mode.
  3. Visibility outside defining translation unit. A function can be local to the translation unit in which it is defined (if the storage class specifier is static). Alternatively, a function can be visible to other translation units (if no storage class is specified, or if the storage class is extern).

  4. Body of the function. You supply the body that executes when the function is called in a single compound statement following the optional declaration-list.

Do not confuse definition with declaration, especially in the case of functions. Function definition implies that the above four pieces of information are supplied. Function declaration implies that the function is defined elsewhere.

You can declare formal parameters as structures or unions. When the function is called, the calling function's argument is copied to temporary locations within the called function.

All functions in C may be recursive. They may be directly recursive so the function calls itself or they may be indirectly recursive so a function calls one or more functions which then call the original function. Indirect recursion can extend through any number of layers.

In function definitions that do not use prototypes, any parameters of type float are actually passed as double, even though they are seen by the body of the function as floats. When such a function is called with a float argument, the float is converted back to float on entry into the function.

NOTE: In compatibility mode, the type of the parameter is silently changed to double, so the reverse conversion does not take place.

In a prototype-style definition, such conversions do not take place, and the float is both passed and accessed in the body as a float.

char and short parameters to nonprototype-style function definitions are always converted to type int. This conversion does not take place in prototype-style definitions.

In either case, arrays of type T are always adjusted to pointer to type T, and functions are adjusted to pointers to functions.

Single dimensioned arrays declared as formal parameters need not have their size specified. If the name of an integer array is x, the declaration is as follows:

int x[ ];

For multidimensional arrays, each dimension must be indicated by a pair of brackets. The size of the first dimension may be left unspecified.

The storage class of formal parameters is implicitly “function parameter.” A further storage class of register is accepted.

Examples

The following example shows a function that returns the sum of an array of integers.

int total(data, n) /* function type, name, formal list */
int data[ ]; /* parameter declarations */
int n;
{
auto int sum = 0; /* local, initialized */
auto int i; /* loop variable */

for(i=0; i<n; ++i) /* range over all elements */
sum += data[i]; /* total the data array */
return sum; /* return the value */
}

This is an example of a function definition without prototypes.

int func1 (p1, p2) /* old-style function definition */
int p1, p2; /* parameter declarations */
{ /* function body starts */
int l1; /* local variables */
11 = p1 + p2;
return l1;
}

Here is an example of a function definition using prototypes.

char *func2 (void) /* new-style definition */
/* takes no parameters */
{
/* body */
}

int func3 (int p1, char *p2, ...)/* two declared parameters:
p1 & p2 */
/* "..." specifies more,
undeclared parameters
of unspecified type */
{
/* body */ /* to access undeclared
parameters here, use the
functions declared in the
<stdarg.h> header file. */
}

inline

HP C supports inlining frequently used functions. These functions can be inlined by specifying them as inline either in the function declarator or in the function definition.

NOTE: inline is available only wit h-AC99. For all other options, you must use __inline.

The optimizer uses its own heuristics to inline a function, if necessary.

Examples

The following examples detail the usage of inline.

Example 3-10 Using inline in Function Declaration

inline void foo(int);     //Function declaration with inline specifier
   main()
   {
      foo(5);
   }
   void foo(int x)
   {
      . . .
   }

Example 3-11 Using inline in Function Definition

   main()
   {
      foo(5);
   }
   inline foo(int x0)      // Function definition with inline specifier
   {
      . . .
   }
© Hewlett-Packard Development Company, L.P.