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

(Romina) #1

Click here to view code image


sales = quantity * price;
return (sales);

is identical to this:


Click here to view code image


return (quantity * price);

The return Data Type


At the beginning of the gradeAve() function, you see float. float is the data type of the
returned value localAverage. You must put the return data type before any function name that
returns a value. If the function returned a long int, long int would have to precede the
function name.


If you don’t specify a return data type, C assumes int. Therefore, C expects that every function
without a return data type specified explicitly will return int. Both of these functions’ first lines
mean exactly the same thing to C:


Click here to view code image


int myFun(int a, float x, char c)

and


Click here to view code image


myFun(int a, float x, char c) /* int is assumed */

Tip

Guess what? Even main() is assumed to return an int value unless you specify an
overriding return data type. That is why you’ve seen return 0; at the end of most
of these programs! Because main() has no specified return data type, int is
assumed, and the return 0; ensures that an int is returned to your operating
system.

If your function doesn’t return a value, or if your function isn’t passed a value, you can insert the
keyword void for either the return data type or the parameter list or both. Therefore, the first line of
a function that neither gets any value nor returns any value might look like this:


Click here to view code image


void doSomething(void) /* Neither is passed nor returns */

Warning

main() can’t be of type void if you use strict American National Standards Institute
Free download pdf