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

(Romina) #1

32. Returning Data from Your Functions


In This Chapter


  • Returning values

  • Using the return data type


This chapter isn’t the end of your C learning—it’s only the beginning. Sounds deep, doesn’t it? This
chapter completes the multiple-function picture by showing you how to return values from the called
function to the calling function. It also explains function prototypes.


The bottom line is this: You will now understand why most programs in this book contain this line:


return 0;


You also will understand the true purpose of header files.


Returning Values


So far, you’ve seen how to send variables to functions. You’re now ready to learn how to return a
value. When a function is to return a value, use the return statement to return the value. C
programmers often put parentheses after the return statement, with the return value inside those
parentheses, such as return (answer);.


Note

If a function doesn’t return a value, a return statement isn’t needed because the
function will return to the calling function automatically. Nevertheless, if you need to
return a value, a return statement is required.

Although you can pass several arguments to functions, you can return only one value to the calling
function. Figure 32.1 explains what is going on. This rule has no exceptions.

Free download pdf