Calling Functions

Functions can be called (executed) from anywhere in a program, and they can receive values as well as return them. A value that you pass (send) to a function is called an “argument.”

Calling a C function is a simple matter. You state the name of the function and supply in parentheses any arguments you want to pass to it. You must place a comma between arguments.

The VOLUME.C program contains two function calls, one to the printf library function and the other to the sphere function, which is defined in the program. The following statement calls the printf function:

printf( "Volume: %f\n", volume );

The statement passes two arguments to printf. The first, "Volume: %f\n", supplies text and some formatting information. The second, volume, supplies a numeric value. See “A Few Words about printf,” below, for more information.

In C, a function does not necessarily have to return a value. It can either return
a value (like a QuickPascal function) or return nothing (like a QuickPascal
procedure).

When a function returns a value, the value is often assigned to a variable. The following statement from VOLUME.C calls the sphere function and assigns its return value to the variable volume:

volume = sphere( radius );

A function uses the return keyword to return a value. In VOLUME.C, the last statement in the sphere function returns the value of the variable result to the statement that calls sphere:

return result;