Sams Teach Yourself C++ in 21 Days

(singke) #1
Exploiting References 269

9


13: cout << “Enter a number (0 - 20): “;
14: cin >> number;
15:
16: error = Factor(number, &squared, &cubed);
17:
18: if (!error)
19: {
20: cout << “number: “ << number << endl;
21: cout << “square: “ << squared << endl;
22: cout << “cubed: “ << cubed << endl;
23: }
24: else
25: cout << “Error encountered!!” << endl;
26: return 0;
27: }
28:
29: short Factor(int n, int *pSquared, int *pCubed)
30: {
31: short Value = 0;
32: if (n > 20)
33: Value = 1;
34: else
35: {
36: *pSquared = n*n;
37: *pCubed = n*n*n;
38: Value = 0;
39: }
40: return Value;
41: }

Enter a number (0-20): 3
number: 3
square: 9
cubed: 27
On line 10,number,squared, and cubedare defined as short integers. numberis
assigned a value based on user input on line 14. On line 16, this number and the
addresses of squaredand cubedare passed to the function Factor().
On line 32,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 the return value,
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 func-
tion returns this value on line 40.

OUTPUT


LISTING9.8 continued


ANALYSIS
Free download pdf