A function call specifies a function name and a list of arguments. The calling function passes the value of each argument to the specified function . The argument list is surrounded by parentheses, and each argument is separated by a comma. The argument list can be empty. When a function is called, the actual arguments are used to initialize the formal arguments.
The type of an actual argument is checked against the type of the corresponding formal argument in the function prototype. All standard and user-defined type conversions are applied as necessary.
For example:
#include <iostream.h> #include <math.h> extern double root(double, double); // declaration double root(double value, double base) // definition { double temp = exp(log(value)/base); return temp; } void main() { int value = 144; int base = 2; // Call function root and print return value cout << "The root is: " << root(value,base) << endl; }
The output is The root is: 12
In the above example, because the function root is expecting arguments of type double, the two int arguments value and base are implicitly converted to type double when the function is called.
The arguments to a function are evaluated before the function is called. When an argument is passed in a function call, the function receives a copy of the argument value. If the value of the argument is an address, the called function can use indirection to change the contents pointed to by the address. If a function or array is passed as an argument, the argument is converted to a pointer that points to the function or array.
Arguments passed to parameters in prototype declarations will be converted to the declared parameter type. For nonprototype function declarations, char and short parameters are promoted to int, and float to double.
You can pass a packed structure argument to a function expecting a nonpacked structure of the same type and vice versa. (The same applies to packed and nonpacked unions.)
Note: If you do not use a function prototype and you send a packed structure when a nonpacked structure is expected, a runtime error may occur.
The order in which arguments are evaluated and passed to the
function is implementation-defined. For example, the following
sequence of statements calls the function tester:
int x; x = 1; tester(x++, x);
The call to tester in the example may produce different
results on different compilers. Depending on the implementation,
x++ may be evaluated first or x may be evaluated first. To avoid
the ambiguity and have x++ evaluated first, replace the preceding
sequence of statements with the following:
int x, y; x = 1; y = x++; tester(y, x);