This section summarizes the syntax and operation of functions.
returnType name ( type1 param1, type2 param2, .. )
The function called name is defined, which returns a value of type returnType and has formal parameters param1,param2, ....param1 is declared to be of type type1,param2 of type type2, and so on.
Local variables are typically declared at the beginning of the function, but that’s not required. They can be declared anywhere, in which case their access is limited to statements appearing after their declaration in the function.
If the function does not return a value, returnType is specified as void.
If just void is specified inside the parentheses, the function takes no arguments. If .. is used as the last (or only) parameter in the list, the function takes a variable number of arguments, as in the following:
int printf (char *format, ...)
Declarations for single-dimensional array arguments do not have to specify the number of elements in the array. For multidimensional arrays, the size of each dimension except the first must be specified.
See the section “The return Statement” for a discussion of the return statement.
An older way of defining functions is still supported.The general format is
returnType name (param1, param2, .. )
Here, just the parameter names are listed inside the parentheses. If no arguments are expected, nothing appears between the left and right parentheses.The type of each parameter is declared outside the parentheses and before the opening curly brace of the
function definition. For example, the following defines a function called rotate that takes
two arguments called value and n:
unsigned int rotate (value, n)
unsigned int value;
The first argument is an unsigned int, and the second is an int.
The keyword inline can be placed in front of a function definition as a hint to the compiler. Some compilers replace the function call with the actual code for the function itself, thus providing for faster execution.An example is shown here:
inline int min (int a, int b)
return ( a < b ? a : b);
name ( arg1, arg2, .. )
The function called name is called and the values arg1,arg2, ... are passed as arguments to the function. If the function takes no arguments, just the open and closed parentheses are specified (as in initialize ()).
If you are calling a function that is defined after the call, or in another file, you should include a prototype declaration for the function, which has the following general format:
returnType name (type1 param1, type2 param2, .. );
This tells the compiler the function’s return type, the number of arguments it takes, and the type of each argument.As an example, the line
long double power (double x, int n);
declares power to be a function that returns a long double and that takes two arguments— the first of which is a double and the second of which is an int.The argument names inside the parentheses are actually dummy names and can be omitted if desired, so
long double power (double, int);
works just as well.
If the compiler has previously encountered the function definition or a prototype declaration for the function, the type of each argument is automatically converted (where possible) to match the type expected by the function when the function is called.
If neither the function’s definition nor a prototype declaration has been encountered, the compiler assumes the function returns a value of type int and automatically converts all float arguments to type double and performs integral promotion on any integer arguments as outlined in the section Conversion of Basic Data Types. Other function arguments are passed without conversion.
Functions that take a variable number of arguments must be declared as such. Otherwise, the compiler is at liberty to assume the function takes a fixed number of arguments based on the number actually used in the call.
If the function were defined with the old-style format (refer to the section “Function Definition”), a declaration for the function takes the following format:
returnType name ();
Arguments to such functions are converted, as described in the previous paragraph.
A function whose return type is declared as void causes the compiler to flag any calls to that function that try to make use of a returned value.
All arguments to a function are passed by value; therefore, their values cannot be changed by the function. If, however, a pointer is passed to a function, the function can change values referenced by the pointer, but it still cannot change the value of the pointer variable itself.
A function name, without a following set of parentheses, produces a pointer to that function. The address operator can also be applied to a function name to produce a pointer to it.
If fp is a pointer to a function, the corresponding function can be called either by writing
If the function takes arguments, they can be listed inside the parentheses.