numbers and data types. (The names of the values don’t have to match.)
You now know everything there is to know about passing parameters and returning values. Put on
your official programmer’s thinking cap and start your C compiler!
The Absolute Minimum
The goal of this chapter was to round out your knowledge of functions by explaining
prototypes and return values. When your program contains a lot of functions, prototype
those functions somewhere before main(). The prototypes tell C what to expect.
After you prototype, you can pass and return variables of any data type. (You can
return ints only if you don’t prototype.)
The prototype ensures that you don’t inadvertently pass the wrong data types to
functions. For example, if the prototype states that you’ll pass two float values to a
function, but you accidentally pass two int variables, C complains. C doesn’t
complain if you don’t prototype, and you might get wrong results because of it.
Now that you know how to return values, you can write functions that mirror those that
are built in, such as sqrt() and rand(). When you call a function, that function
returns a value based on the function’s code. A function can return a maximum of one
value, just like functions that are built in. Key concepts from this chapter include:
- Place the return data type before a function name that returns a value.
- The return value appears after a return statement.
- In the calling function, do something with the return value. Print it or assign it to
something. Calling a function that returns a value is useless if you do nothing with
the return value. - Use void as the return data type or in the parameter list if you neither return nor
pass values to a function. - Don’t return more than one value from a function.
- Don’t return a non-integer without a prototype. Better yet, prototype all functions
except main().