O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating Functions That Return a Value

The final addition to making your functions as practical as the standard C library functions is to have them return values. This is accomplished using the return statement, which you've already been using in the main function. You can return any single value from your function:

return 1;

The returned value can even be based on a variable's value:

int num = 8;
return num;

Any function that returns a value must have a proper prototype and definition, both of which must indicate the type of value returned. You've already seen this with the main function (which returns an int). Here's another sample function definition:

int my_func (void) {
   return 27;
}

You can assign the returned values to variables when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required