Declaring a Function's Return Type

Thus far, we have explained how a function can return a value—and how the calling statement can use that value—without paying much attention to what type of value the function returns. (The C language supports various data types, such as int for integer values, and float for floating-point values. For a detailed description of data types, see Chapter 4.)

The return type is important because it controls what the function returns. If a function returns an integer when you expect a floating-point value, your program may not work correctly.

Summary: A function's prototype and definition control what type of value it returns.

The function's return type is specified in its prototype and definition. Below are the prototype and definition of the sphere function from the VOLUME.C program in Chapter 1, “Anatomy of a C Program.” They specify that the function returns a float value.

float sphere( int rad ); /* function prototype */

float sphere( int rad ) /* function header */

The type name (here, float) in front of the function name shows what type of value the function returns. If the sphere function returned an int value, its prototype and header would look like this:

int sphere( int rad ); /* function prototype */

int sphere( int rad ) /* function header */

Summary: Use the void type name to show that a function returns no value.

You should declare the return type for every function—even for functions that don't return a value. These functions are declared with the void type name. In the SHOWME.C program, shown above, the prototype of the showme function follows this pattern:

void showme( int a, int b, int c );

The void that precedes the function name indicates that showme returns
nothing.