Returning Multiple Values

As discussed, functions can only return one value. What if you need to get two values back from a function? One way to solve this problem is to pass two objects into the function, by reference. The function can then fill the objects with the correct values. Because passing by reference enables a function to change the original objects, this effectively lets the function return two pieces of information. This approach bypasses the return value of the function, which can then be reserved for reporting errors.

Once again, this can be done with references or pointers. Listing 11.7 demonstrates a function that returns three values, two as pointer parameters and one as the return value of the function.

Listing 11.7. Returning Values with Pointers
 0:  //Listing 11.7
 1:  // Returning multiple values from a function
 2:  #include <iostream>
 3:
 4:  short Factor(int, int*, int*);
 5:
 6:  int main()
 7:  {
 8:      int number, squared, cubed;
 9:      short error;
10:
11:      std::cout << "Enter a number (0 - 20): ";
12:      std::cin >> number;
13:
14:      error = Factor(number, &squared, &cubed);
15:
16:      if (!error)
17:      {
18:          std::cout << "number: " << number << "
";
19:          std::cout << "square: " << squared << "
";
20:          std::cout << "cubed: "  << cubed   << "
";
21:      }
22:      else
23:          std::cout << "Error encountered!!
";
24:      return 0;
25:  }
26:
27:  short Factor(int n, int *pSquared, int *pCubed)
28:  {
29:      short Value = 0;
30:      if (n > 20)
31:          Value = 1;
32:      else
33:      {
34:          *pSquared = n*n;
35:          *pCubed = n*n*n;
36:          Value = 0;
37:      }
38:      return Value;
39:  }


Enter a number (0-20): 3
number: 3
square: 9
cubed: 27
					

On line 8, number, squared, and cubed are defined as int. number is assigned a value based on user input. This number and the addresses of squared and cubed are passed to the function Factor().


Factor() examines the first parameter, which is passed by value. If it is greater than 20 (the maximum value this function can handle), it sets return Value to a simple error value. Note that the return value from Function() is reserved for either this error value or the value 0, indicating all went well; also note that the function returns this value on line 38.

The actual values needed, the square and cube of number, are returned not by using the return mechanism, but rather by changing the values directly using the pointers that were passed into the function.

On lines 34 and 35, the pointers are assigned their return values. On line 36, return Value is assigned a success value. On line 38, return Value is returned.

One improvement to this program might be to declare the following:

enum ERROR_VALUE { SUCCESS, FAILURE};

Then, rather than returning 0 or 1, the program could return SUCCESS or FAILURE. As you saw earlier, the first enumerated value (SUCCESS) will be given the value 0 and the second will be given the value 1.

Returning Values by Reference

Although Listing 11.7 works, it can be made easier to read and maintain by using references rather than pointers. Listing 11.8 shows the same program rewritten to use references and to incorporate the ERROR enumeration.

Listing 11.8. Listing 11.7 Rewritten Using References
 0:  //Listing 11.8
 1:  // Returning multiple values from a function
 2:  // using references
 3:  #include <iostream>
 4:
 5:  enum ERR_CODE { SUCCESS, ERROR };
 6:
 7:  ERR_CODE Factor(int, int&, int&);
 8:
 9:  int main()
10:  {
11:      int number, squared, cubed;
12:      ERR_CODE result;
13:
14:      std::cout << "Enter a number (0 - 20): ";
15:      std::cin >> number;
16:
17:      result = Factor(number, squared, cubed);
18:
19:      if (result == SUCCESS)
20:      {
21:          std::cout << "number: " << number << "
";
22:          std::cout << "square: " << squared << "
";
23:          std::cout << "cubed: "  << cubed   << "
";
24:      }
25:      else
26:          std::cout << "Error encountered!!
";
27:      return 0;
28:  }
29:
30:  ERR_CODE Factor(int n, int &rSquared, int &rCubed)
31:  {
32:      if (n > 20)
33:          return ERROR;   // simple error code
34:      else
35:      {
36:          rSquared = n*n;
37:          rCubed = n*n*n;
38:          return SUCCESS;
39:      }
40:  }


Enter a number

 (0-20): 3
number: 3
square: 9
cubed: 27
						

Listing 11.8 is identical to 11.7 , with two exceptions. The ERR_CODE enumeration makes the error reporting a bit more explicit on lines 33 and 38, as well as the error handling on line 19.


The larger change, however, is that Factor() is now declared to take references to squared and cubed rather than pointers. This makes the manipulation of these parameters far simpler and easier to understand.

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

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