HP C A.06.05 Reference Manual
Expressions and Operators
Function Calls
Chapter 5 107
The compiler checks to see that there are as many arguments as required by the function
prototype. If the prototype has an ellipsis, additional parameters are allowed. Otherwise, they
are flagged erroneous. Also, the types of the arguments must be assignment-compatible with
their corresponding formal parameters, or the compiler will emit a diagnostic message.
If the function does not have a prototype, then the arguments are evaluated and subjected to
the default argument promotions; that is, arguments of type char or short (both signed and
unsigned) are promoted to type int, and float arguments are promoted to double.
In this case, the compiler does not do any checking between the argument types and the types
of the parameters of the function (even if it has seen the definition of the function). Thus, for
safety, it is highly advisable to use prototypes wherever possible.
In both cases, arrays of type T are converted to pointers to type T, and functions are converted
to pointers to functions.
Function Formal Parameters
Within a function, the formal parameters are lvalues that can be changed during the function
execution. This does not change the arguments as they exist in the calling function. It is
possible to pass pointers to objects as arguments. The called function can then reference the
objects indirectly through the pointers. The result is as if the objects were passed to the
function using call by reference. The following swap function illustrates the use of pointers as
arguments. The swap() function exchanges two integer values:
void swap(int *x,int *y)
{
int t;
t = *x;
*x = *y;
*y = t;
}
To swap the contents of integer variables i and j, you call the function as follows:
swap(&i, j);
Notice that the addresses of the objects (pointers to int) were passed and not the objects
themselves.
Because arrays of type T are converted into pointers to type T, you might think that arrays
are passed to functions using call by reference. This is not actually the case. Instead, the
address of the first element is passed to the called function. This is still strictly call by value
since the pointer is passed by value. Inside the called function, references to the array via the
passed starting address, are actually references to the array in the calling function. Arrays
are not copied into the address space of the called function.