When Colonel John Roebling designed the Brooklyn Bridge, he worried in detail about
how the concrete was poured and how the wire for the bridge was manufactured. He was
intimately involved in the mechanical and chemical processes required to create his
materials. Today, however, engineers make more efficient use of their time by using well-
understood building materials, without regard to how their manufacturer produced them.
It is the goal of C++ to enable programmers to rely on well-understood classes and func-
tions without regard to their internal workings. These “component parts” can be assem-
bled to produce a program, much the same way wires, pipes, clamps, and other parts are
assembled to produce buildings and bridges.
In much the same way that an engineer examines the spec sheet for a pipe to determine
its load-bearing capacity, volume, fitting size, and so forth, a C++ programmer reads the
declaration of a function or class to determine what services it provides, what parameters
it takes, and what values it returns.
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 func-
tion, by reference. The function can then fill the objects with the correct values. Because
passing by reference allows a function to change the original objects, this effectively
enables the function to 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.
LISTING9.8 Returning Values with Pointers
1: //Listing 9.8 - Returning multiple values from a function
2:
3: #include <iostream>
4:
5: using namespace std;
6: short Factor(int n, int* pSquared, int* pCubed);
7:
8: int main()
9: {
10: int number, squared, cubed;
11: short error;
12:
268 Day 9