Inline Functions


One of the reasons we use functions is to conserve memory. That is if a collection of code appears repeatedly in a program, it can be replaced with a function and thereby reducing the size of the program with calls to the same function. However, there is a sacrifice for this conservation. The more times a function is called, potentially, the more processor time is required to execute than the code it replaced. It takes processor time to jump from the flow of the code to the function's definition and back. This is in addition to the running of the code itself. This time would not be spent if there was no function.

In C, macros are used to address this problem. Macros make the code look like a function was used but the "call" is replaced with the "body" when the program is compiled by the preprocessor. Macros can give the organization of a function without reduced speed. But macros expand the size of the code and are based upon #define that lacks type checking and can introduce bugs. C++ takes advantage of both of these constructs with INLINE FUNCTIONS. These functions organize code into blocks like functions and do type checking. However, when compiled the body is expanded back into the program to replace the function call just like a macro.

Inline functions are different from macros in another way. Specifying a function to be inline is only a request and not a demand. That is, if the compiler determines that the request will expand the code too much (or some other unacceptable condition will occur), the compiler will not expand the program or replace all calls of the function with the body of the function. In this case, inline functions are treated like other functions. Which inline function requests are denied is compiler specific. For example, an earlier version of a Borland C++ compiler would not permit an expansion of an inline function that contained the for(; ;) construct even though the Microsoft compiler at that time did.

To enable the expansion, only functions with a small body should be defined as inline. A function becomes this type when the keyword inline precedes the definition. In this case the function definition must be the declaration (except for classes as will be discussed later). The construct of an inline function definition is:

image from book

 inline ouput_type f(signature) {    ... } 

image from book

For example:

image from book

 inline double MAX(double x, double y) { return ((x > y) ? x : y); } 

image from book

then expands the call:

image from book

 double c = MAX(a,b); 

image from book

into the following code:

image from book

 double c = ((x > y) ? x : y); 

image from book

There are problems with inline including: too many inlines may expand excessively the memory required for the function. In addition, inline functions in header files may require recompiling if they are changed. It should be noted that functions that are inline have static linkage and do not expand to the entire program. See INLINE1.CPP, and INLINE2.CPP




Intermediate Business Programming with C++
Intermediate Business Programming with C++
ISBN: 738453099
EAN: N/A
Year: 2007
Pages: 142

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