Arguments to functions

In the declaration of a function it is necessary to define the types of any arguments or parameters which are to be supplied when the function is called. These can be used as variables of the corresponding type within the function, unless they are specified as const values, but start out with the value passed in the call.

The list in a declaration is known as the formal parameters or arguments of the function.

The list in the call is known as the actual parameters.

Any changes made to the values of parameters within the body of the function have no effect on the values of variables passed as actual parameters. Their values are copied into local variable locations.

The declaration of a function which is going to require parameters (sometimes called arguments) has to list those parameters in the parentheses which follow the function name. Parameter names listed after their associated types is very similar to variable declarations, except that individual parameters are separated by commas not semi-colons. Note also that a separate type specifier must be given for each parameter name.

Functions without arguments must be declared with an empty argument list, i.e. with empty parentheses following the function name.

Examples of argument lists

No arguments
int give_three()
{
    return 3;
}
One argument
void out_one(int i)
{
    printf("%d\n",i);
}
Mixed arguments
float make_real(int i, int j, float r)
{
    return (float)(i+j) + r;
}

Exercises on this section.


Next - Generating values in functions.

Back to Contents page.