C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
FIGURE 32.1 You can pass more than one value but return only one.

Although a single return value might seem limiting, it really isn’t. Consider the built-in
sqrt()function. You might remember from Chapter 20, “Advanced Math (For the Computer, Not
You!),” that sqrt() returns the square root of whatever value is passed to it. sqrt() doesn’t
return several values—only one. As a matter of fact, none of the built-in functions returns more than a
single value, and neither can yours.


Note

The gets() function seems as if it returns more than one value because it returns a
character string array. Remember, though, that an array name is nothing more than a
pointer to the array’s first position. Therefore, gets() actually returns a character
pointer that points to the beginning of the string the user entered.

The following program contains a function that receives three floating-point values: test1, test2,
and test3. The function named gradeAve() calculates the average of those three grades and then
returns the answer.


Click here to view code image


// Example program #1 from Chapter 32 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter32ex1.c
/* The program demonstrates functions returning a value by passing
three floating-point numbers (grades) and calculating the average of
the three. */
Free download pdf