Sams Teach Yourself C++ in 21 Days

(singke) #1

More About 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 returnfollowed 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 returnstatements, assuming that the function MyFunction()itself
returns a value. The value in the second statement,return (x > 5), will be falseif xis
not greater than 5 , or it will be true. What is returned is the value of the expression,
falseor true, not the value of x.
When the returnkeyword is encountered, the expression following returnis returned
as the value of the function. Program execution returns immediately to the calling func-
tion, and any statements following the return are not executed.
It is legal to have more than one return statement in a single function. Listing 5.6 illus-
trates this idea.

LISTING5.6 A Demonstration of Multiple Return Statements
1: // Listing 5.6 - demonstrates multiple return
2: // statements
3: #include <iostream>
4:
5: int Doubler(int AmountToDouble);
6:
7: int main()
8: {
9: using std::cout;
10:
11: int result = 0;
12: int input;
13:
14: cout << “Enter a number between 0 and 10,000 to double: “;
15: std::cin >> input;
16:
17: cout << “\nBefore doubler is called... “;
18: cout << “\ninput: “ << input << “ doubled: “ << result << “\n”;
19:
20: result = Doubler(input);
21:
22: cout << “\nBack from Doubler...\n”;

114 Day 5

Free download pdf