Function Calls [ HP C/iX Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Reference Manual
Function Calls
Function calls provide a means of invoking a function and passing
arguments to it.
Syntax
postfix-expression ( [argument-expression-list] )
Description
The postfix-expression must have the type "pointer to function returning
T." The result of the function will be type T. Functions can return any
type of object except array and function. Specifically, functions can
return structures. In the case of structures, the contents of the
returned structure is copied to storage in the calling function. For
large structures, this can use a lot of execution time.
Although the expression denoting the called function must actually be a
pointer to a function, in typical usage, it is simply a function name.
This works because the function name will automatically be converted to a
pointer, as explained in chapter 4.
C has no call statement. Instead, all function references must be
followed by parentheses. The parentheses contain any arguments that are
passed to the function. If there are no arguments, the parentheses must
still remain. The parentheses can be thought of as a postfix call
operator.
If the function name is not declared before it is used, the compiler
enters the default declaration:
extern int identifier();
Function arguments are expressions. Any type of object can be passed to
a function as an argument. Specifically, structures can be passed as
arguments. Structure arguments are copied to temporary storage in the
called function. The length of time required to copy a structure
argument depends upon the structure's size.
If the function being called has a prototype, each argument is evaluated
and converted as if being assigned to an object of the type of the
corresponding parameter. If the prototype has an ellipsis, any argument
specified after the fixed parameters is subject to the default argument
promotions described below.
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 are 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.
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.
All functions are recursive both in the direct and indirect sense.
Function A can call itself directly or function A can call function B
which, in turn, calls function A. Note that each invocation of a function
requires program stack space. For this reason, the depth of recursion
depends upon the size of the execution stack.
MPE/iX 5.0 Documentation