Return Values

Functions return a value or return void. void is a signal to the compiler that no value will be returned.

To return a value from a function, write the keyword return followed by the value you want to return. The value might itself be an expression that returns a value. For example:

return 5;
return (x > 5);
return (MyFunction());

These are all legal return statements, assuming that the function MyFunction() itself returns a value. The value in the second statement return (x > 5), will be false if x is not greater than 5, or it will be true. What is returned is the value of the expression, false or true, not the value of x.

When the return keyword is encountered, the expression following return is returned as the value of the function. ...

Get Sams Teach Yourself C++ in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.