16: cout << “. Square(“ << x << “): “;
17: cout << Square(x);
18: cout<< “. Cube(“ << x << “): “;
19: cout << Cube(x) << “.” << endl;
20: }
21: return 0;
22: }
Enter a number (0 to quit): 1
You entered: 1. Square(1): 1. Cube(1): 1.
Enter a number (0 to quit): 2
You entered: 2. Square(2): 4. Cube(2): 8.
Enter a number (0 to quit): 3
You entered: 3. Square(3): 9. Cube(3): 27.
Enter a number (0 to quit): 4
You entered: 4. Square(4): 16. Cube(4): 64.
Enter a number (0 to quit): 5
You entered: 5. Square(5): 25. Cube(5): 125.
Enter a number (0 to quit): 6
You entered: 6. Square(6): 36. Cube(6): 216.
On lines 3 and 4, two inline functions are defined:Square()and Cube(). Each is
declared to be inline, so like a macro function, these are expanded in place for
each call, and no function call overhead occurs.
As a reminder, expanded inline means that the content of the function is placed into the
code wherever the function call is made (for example, on line 17). Because the function
call is never made, there is no overhead of putting the return address and the parameters
on the stack.
On line 17, the function Squareis called, as is the function Cubeon line 19. Again,
because these are inline functions, it is exactly as if this line had been written like this:
16: cout << “. Square(“ << x << “): “ ;
17: cout << x * x ;
18: cout << “. Cube(“ << x << “): “ ;
19: cout << x * x * x << “.” << endl;
OUTPUT
772 Day 21
LISTING21.6 continued
ANALYSIS
DOuse CAPITALS for your macro names.
This is a pervasive convention, and other
programmers will be confused if you
don’t.
DOsurround all arguments with paren-
theses in macro functions.
DON’Tallow your macros to have side
effects. Don’t increment variables or
assign values from within a macro.
DON’T use #definevalues when a con-
stant variable will work.
DO DON’T