Printf and scanf have return values

9.15 The return value of functions

Now you've seen how it is possible to pass data into a function. Now, of course, you want to know how to write a function to return data to the caller.

First you need a function definition with a return value. You can use any data type as return value, for example:

int bignum (int a, int b) {if (a> b) return a; else if (a

You can see from this function by prefixing the data type that the return value is an integer. And in the function itself there is an instruction

a value is returned to the calling function. This return statement is not only used to return a value to the calling function, it also defines the end of a function. With return, control is returned to the calling function. A value can be returned. Now the caller itself is needed with its arguments:

int big; ... big = bignum (value1, value2);

This means that the variable big, which must be of the data type int, is assigned the return value of the function bignum (). The complete program would then look like this:

/ * bignum.c * / #include int bignum (int a, int b) {if (a> b) return a; else if (a

In the further course you will get to know some possibilities of functions with different return values.

your opinion

How did you like the Openbook? We always look forward to your feedback. Please send us your feedback as an e-mail to [email protected]