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. Program execution returns immediately to the calling function, and any statements following the return are not executed.

It is legal to have more than one return statement in a single function. However, keep in mind that as soon as a return statement is executed, the function ends. Listing 5.5 illustrates this idea.

Listing 5.5. Demonstrates Multiple Return Statements
 0:  // Listing 5.5 - demonstrates multiple return
 1:  // statements
 2:  #include <iostream>
 3:
 4:  int Doubler(int AmountToDouble);
 5:
 6:  int main()
 7:  {
 8:      int result = 0;
 9:      int input;
10:
11:      std::cout << "Enter a number between 0 and "
12:                << "10,000 to double: ";
13:      std::cin >> input;
14:
15:      std::cout << "
Before doubler is called...";
16:      std::cout << "
input: " << input
17:                << " doubled: " << result << "
";
18:
19:      result = Doubler(input);
20:
21:      std::cout << "
Back from Doubler...";
22:      std::cout << "
input: " << input
23:                << " doubled: " << result << "

";
24:
25:      return 0;
26:  }
27:
28:  int Doubler(int original)
29:  {
30:      if (original <= 10000)
31:          return original * 2;
32:      else
33:          return -1;
34:      std::cout << "You can't get here!
";
35:  }
					


Enter a number between 0 and 10,000 to double: 9000

Before doubler is called...
input: 9000 doubled: 0

Back from doubler...
input: 9000 doubled: 18000


Enter a number between 0 and 10,000 to double: 11000

Before doubler is called...
input: 11000 doubled: 0

Back from doubler...
input: 11000 doubled: -1

A number is requested on lines 11, 12 and 13 and printed on lines 16 and 17, along with the local variable result. The function Doubler() is called on line 19, and the input value is passed as a parameter. The result will be assigned to the local variable result, and the values will be reprinted on lines 21, 22 and 23.


On line 30, in the function Doubler(), the parameter is tested to see whether it is greater than 10,000. If it is not, the function returns twice the original number. If it is greater than 10,000, the function returns -1 as an error value.

The statement on line 34 is never reached, because whether or not the value is greater than 10,000, the function returns before it gets to line 34, on either line 31 or line 33. Many compilers will warn that this statement cannot be executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.157