Inline Function in C++ Programming

One of the objectives of using functions in a program is to save some memory space which becomes appreciable when a function is likely to be called many times. However, every time a function is called, it takes a lot of extra time in executing a series of instructions for tasks such as jumping to the function, saving registers, pushing arguments into the stack, and returning to the calling function. When a function is small, a substantial percentage of execution time may be spent in such overheads.

One solution to this problem is to use macro definitions, popularly known as macros. Preprocessor macros are popular in C. The major drawback with macros is that they are not really functions and therefore, the usual error checking does not occur during compilation.

C++ has a different solution to this problem. To eliminate the cost of calls to small functions, C++ proposes a new feature called inline function. An inline function is a function that is expanded in line when it is invoked. That is the compiler replaces the function call with the corresponding function code (something similar to macros expansion). The inline functions are defined as follows: 

inline function header
{
    function body
}

Example:

inline double cube(double a)
{
    return{a*a*a);
}

The above inline function can he invoked by statements like
c = cube (3.0);
d = cube (2.5+1.5);
On the execution of these statements, the values of c and d will be 27 and 64 respectively. If the arguments are expressions such as 2.5 + 1.5, the function passes the value of the expression, 4 in this case. This makes the inline feature far superior to macros.

It is easy to make a function inline. All we need to do is to prefix the keyword inline to the function definition. All inline functions must be defined before they are called.

We should exercise care before making a function inline. The speed benefits of inline functions diminish as the function grows in size. At some point, the overhead of the function call becomes small compared to the execution of the function, and the benefits of inline functions may he lost. In such cases, the use of normal functions will be more meaningful. Usually, the functions are made inline when they are small enough to be defined in one or two lines. Example:

inline double cube (double a) {return(a*a*a);}

Remember that the inline keyword merely sends a request, not a command, to the compiler. The compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function.

Some of the situations where inline expansion may not work are:
  • For functions returning values, if a loop, a switch, or a goto exists.
  • For functions not returning values, if a return statement exists.
  • If functions contain static variables.
  • If inline functions are recursive.

Conclusion
Inline expansion makes a program run faster because the overhead of a function call and return is eliminated. However, it makes the program to take up more memory because the statements that define the inline function are reproduced at each point where the function is called. So, a trade-off becomes necessary.

No comments:

Post a Comment