|

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. Since
passing by reference allows 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 9.8 demonstrates a function that returns
three values: two as pointer parameters and one
as the return value of the function.
Listing
9.8. Returning values with pointers.
1:
//Listing 9.8
2:
// Returning multiple values from a function
3:
4:
#include <iostream.h>
5:
6:
typedef unsigned short USHORT;
7:
8:
short Factor(USHORT, USHORT*, USHORT*);
9:
10:
int main()
11:
{
12:
USHORT number, squared, cubed;
13:
short error;
14:
15:
cout << "Enter a number (0 - 20): ";
16:
cin >> number;
17:
18:
error = Factor(number, &squared, &cubed);
19:
20:
if (!error)
21:
{
22:
cout << "number: " << number <<
"\n";
23:
cout << "square: " << squared <<
"\n";
24:
cout << "cubed: " << cubed <<
"\n";
25:
}
26:
else
27:
cout << "Error encountered!!\n";
28:
return 0;
29:
}
30:
31:
short Factor(USHORT n, USHORT *pSquared, USHORT
*pCubed)
32:
{
33:
short Value = 0;
34:
if (n > 20)
35:
Value = 1;
36:
else
37:
{
38:
*pSquared = n*n;
39:
*pCubed = n*n*n;
40:
Value = 0;
41:
}
42:
return Value;
43:
}
OUTPUT
Enter a number (0-20): 3
number: 3
square: 9
cubed: 27
ANALYSIS: On line 12, number, squared,
and cubed are defined as USHORTs. 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, and note that the function returns
this value on line 42.
The
actual values needed, the square and cube of number,
are returned not by using the return mechanism,
but rather by changing the pointers that were passed
into the function.
On
lines 38 and 39, the pointers are assigned their
return values. On line 40, return Value is assigned
a success value. On line 41, 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.
Returning
Values by Reference
Although
Listing 9.8 works, it can be made easier to read
and maintain by using references rather than pointers.
Listing 9.9 shows the same program rewritten to
use references and to incorporate the ERROR enumeration.
Listing
9.9.Listing 9.8 rewritten using references.
1:
//Listing 9.9
2:
// Returning multiple values from a function
3:
// using references
4:
5:
#include <iostream.h>
6:
7:
typedef unsigned short USHORT;
8:
enum ERR_CODE { SUCCESS, ERROR };
9:
10:
ERR_CODE Factor(USHORT, USHORT&, USHORT&);
11:
12:
int main()
13:
{
14:
USHORT number, squared, cubed;
15:
ERR_CODE result;
16:
17:
cout << "Enter a number (0 - 20): ";
18:
cin >> number;
19:
20:
result = Factor(number, squared, cubed);
21:
22:
if (result == SUCCESS)
23:
{
24:
cout << "number: " << number <<
"\n";
25:
cout << "square: " << squared <<
"\n";
26:
cout << "cubed: " << cubed <<
"\n";
27:
}
28:
else
29:
cout << "Error encountered!!\n";
30:
return 0;
31:
}
32:
33:
ERR_CODE Factor(USHORT n, USHORT &rSquared,
USHORT &rCubed)
34:
{
35:
if (n > 20)
36:
return ERROR; // simple error code
37:
else
38:
{
39:
rSquared = n*n;
40:
rCubed = n*n*n;
41:
return SUCCESS;
42:
}
43:
}
OUTPUT:
Enter a number (0 - 20): 3
number: 3
square: 9
cubed: 27
ANALYSIS: Listing 9.9 is identical
to 9.8, with two exceptions. The ERR_CODE enumeration
makes the error reporting a bit more explicit on
lines 36 and 41, as well as the error handling on
line 22.
The
larger change, however, is that Factor() is now
declared to take references to squared and cubed
rather than to pointers. This makes the manipulation
of these parameters far simpler and easier to understand.
|