Home     Blog

Inline Function

One of the main 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 task such as jumping to the function, saving register, 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 definition, popularly know as macros. Preprocessor macros are popular in C. The major drawback with macro is that they are not really functions, and therefore, the usual error checking doe not occur during compilation.

C++ has a special 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 codes. The inline function is defined as follows:

Example:

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

inline functions clip image002 Inline Function

Repeated code substituted in place of the function call.

inline functions clip image004 Inline Function

We should take utmost care while making a function inline. The speed benefit of an inline function diminishes as it grows in size. At some point the overheads of the function call becomes small as compared to the execution of the function, and the benefits of inline function may be lost. In such a case, the use of normal function will be more meaningful. Normally only those functions are defined inline, which are only two or three lines big.

The keyword inline sends a request to the compiler to make the function inline and not a command.

There are few situations where an inline function may not work:

  • For a function returning values; if a return statement exists.
  • For a function not returning any values; if a loop, switch or goto statement exists.
  • If a function is recursive.

Example: Program to illustrate the working of an inline function.

inline functions clip image006 Inline Function

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Comments (1)

 

  1. suresh says:

    good and useful.. but less examples..
     

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Function Template Overloading

    A template function may be overloaded either by template functions or ordinary functions. In such cases, the overloading resolution is accomplished using following steps: Call an ordinary function that has an exact match. If a match for an ordinary function is not found, call a template function that could be created with an exact match. [...]...


  • Friend Function

    The private data members of a class cannot be accessed by functions that are not a part of the class. The access to such members can only be given to the functions by specifying the function as friend of the class whose data structures are required by the function. In simple words, we can say [...]...


  • Function Overloading

    Function overloading is a concept where several function declarations are specified with a single and a same function name within the same scope. Such functions are said to be overloaded. C++ allows functions to have the same name. Such functions can only be distinguished by their number and type of arguments. Example: float divide(int a, [...]...


  • Function Templates

    Like class templates, we can also define function templates that could be used to create a family of functions with different argument types. The general format of a function template is as follows: template <class T> return-type function-name( argument of type T ) { // body of function with Type T } The function template [...]...


  • Functions

    A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program. A programmer can solve a simple problem in C++ with a single function. Difficult and complex problems can be decomposed into sub-problems, each of which can be either coded directly or further [...]...