HP C A.06.05 Reference Manual
Data Types and Declarations
Function Definitions
Chapter 372
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. */
}