Function Call Operator ( )

A function call is an expression containing a simple type name and a parenthesized argument list. The argument list can contain any number of expressions separated by commas. It can also be empty.

For example:

stub()
overdue(account, date, amount)
notify(name, date + 5)
report(error, time, date, ++num)

There are two kinds of function calls: ordinary function calls and C++ member function calls. Any function may call itself except for the function main.

Type of a Function Call

The type of a function call expression is the return type of the function. This type can either be a complete type, a reference type, or the type void. A function call is an lvalue if and only if the type of the function is a reference.

Arguments and Parameters

A function argument is an expression that you use within the parentheses of a function call. A function parameter is an object or reference declared within the parentheses of a function declaration or definition. When you call a function, the arguments are evaluated, and each parameter is initialized with the value of the corresponding argument. The semantics of argument passing are identical to those of assignment.

A function can change the values of its non-const parameters, but these changes have no effect on the argument unless the parameter is a reference type.

Linkage and Function Calls

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 explicitly declared because an implicit declaration of extern int func(); is assumed. This is not true for C++.

Type Conversions of Arguments

Arguments that are arrays or functions are converted to pointers before being passed as function arguments.

Arguments passed to nonprototyped C functions undergo conversions: type short or char parameters are converted to int, and float parameters to double. Use a cast expression for other conversions.

The compiler compares the data types provided by the calling function with the data types that the called function expects and performs necessary type conversions. For example, when function funct is called, argument f is converted to a double, and argument c is converted to an int:

char * funct (double d, int i);
     /* ... */
int main(void)
{
   float f;
   char c;
   funct(f, c) /* f is converted to a double, c is converted to an int */
   return 0;
}

Evaluation Order of Arguments

The order in which arguments are evaluated is not specified. Avoid such calls as:

method(sample1, batch.process--, batch.process);

In this example, batch.process-- might be evaluated last, causing the last two arguments to be passed with the same value.

Example of Function Calls

In the following example, main passes func two values: 5 and 7. The function func receives copies of these values and accesses them by the identifiers: a and b. The function func changes the value of a. When control passes back to main, the actual values of x and y are not changed. The called function func only receives copies of the values of x and y, not the variables themselves.

/**
 ** This example illustrates function calls
 **/
 
#include <stdio.h>
 
void func (int a, int b)
{
   a += b;
   printf("In func, a = %d    b = %d\n", a, b);
}
 
int main(void)
{
   int x = 5, y = 7;
   func(x, y);
   printf("In main, x = %d    y = %d\n", x, y);
   return 0;
}
 

This program produces the following output:

In func, a = 12   b = 7
In main, x = 5    y = 7

Related References

IBM Copyright 2003