Inline Functions

Implementing a program as a set of functions is good from a software engineering standpoint, but function calls involve execution-time overhead. C++ provides inline functions to help reduce function call overheadespecially for small functions. Placing the qualifier inline before a function's return type in the function definition "advises" the compiler to generate a copy of the function's code in place (when appropriate) to avoid a function call. The trade-off is that multiple copies of the function code are inserted in the program (often making the program larger) rather than there being a single copy of the function to which control is passed each time the function is called. The compiler can ignore the inline qualifier and typically does so for all but the smallest functions.


Software Engineering Observation 6.11

Any change to an inline function could require all clients of the function to be recompiled. This can be significant in some program development and maintenance situations.

Good Programming Practice 6.5

The inline qualifier should be used only with small, frequently used functions.

Performance Tip 6.4

Using inline functions can reduce execution time but may increase program size.

Figure 6.18 uses inline function cube (lines 1114) to calculate the volume of a cube of side side. Keyword const in the parameter list of function cube (line 11) tells the compiler that the function does not modify variable side. This ensures that the value of side is not changed by the function when the calculation is performed. (Keyword const is discussed in detail in Chapter 7, Chapter 8 and Chapter 10.) Notice that the complete definition of function cube appears before it is used in the program. This is required so that the compiler knows how to expand a cube function call into its inlined code. For this reason, reusable inline functions are typically placed in header files, so that their definitions can be included in each source file that uses them.

Figure 6.18. inline function that calculates the volume of a cube.

(This item is displayed on pages 274 - 275 in the print version)

 1 // Fig. 6.18: fig06_18.cpp
 2 // Using an inline function to calculate the volume of a cube.
 3 #include 
 4 using std::cout;
 5 using std::cin;
 6 using std::endl;
 7
 8 // Definition of inline function cube. Definition of function appears
 9 // before function is called, so a function prototype is not required.
10 // First line of function definition acts as the prototype.
11 inline double cube( const double side ) 
12 { 
13  return side * side * side; // calculate cube
14 } // end function cube 
15
16 int main()
17 {
18 double sideValue; // stores value entered by user
19 cout << "Enter the side length of your cube: ";
20 cin >> sideValue; // read value from user
21
22 // calculate cube of sideValue and display result
23 cout << "Volume of cube with side "
24 << sideValue << " is " << cube( sideValue ) << endl;
25 return 0; // indicates successful termination
26 } // end main
 
 Enter the side length of your cube: 3.5
 Volume of cube with side 3.5 is 42.875
 

Software Engineering Observation 6.12

The const qualifier should be used to enforce the principle of least privilege. Using the principle of least privilege to properly design software can greatly reduce debugging time and improper side effects and can make a program easier to modify and maintain.


Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

Templates

Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger

Bibliography



C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net