INLINE FUNCTIONS IN C++

INLINE FUNCTIONS IN C++

         Functions used in the program helps to reduce the size of the program as multiple calls to a function causes the execution of the same set of instructions which appears only once in the memory without any duplication.  However, each time a function is called it involves substantial execution time overheads for tasks such as storing memory address of the instruction following the function call, saving values ​​of registers, pushing actual arguments onto the stack and passing control to the function where the arguments of  the called function are popped (removed) from the stack.  Then the body of function executes.  If there is any return statement, the value is returned to the calling program.  Finally, the control is transferred back to the calling program.  All these overheads increases the execution time and consequently reduces the efficiency of the program.  
          In case of functions containing large lines of code, the execution time overheads cost can be compromised with the huge amount of memory saved.  But in case of functions containing few instructions, there is little saving in memory space although the execution time overheads remains as that of a large function.  
          One solution to this problem is to replace each function call with the necessary code instead of making function.  This results in improving the execution time as there is no transfer of control but using the same repeated code in the program suffers from the advantages of functions such as modularity, understandability etc.  Any modification will also cause problems.  So in order to take the benefits of functions and at the same time improve the execution time in case of small functions, C ++ provides another type of function known as Inline function. 
          Inline function is a function which gets expanded inline at each point in the  program where it is called.  In other words, inline functions are those functions whose function body is inserted in place of the function call during the compilation process.  Thus, there is no transfer of control between the calling and the called function that results in removing the function call overheads which in turn improves the execution time.
          Functions are made inline normally when they are small and contains only few lines of code.  The use of inline function improves the execution speed of the program and also there is not much increase in the size of program as it is a small function.  As the function grows in size, the execution time benefits of inline functions becomes a very costly affair.  Inline function containing large lines of code still provides execution time benefits but due to huge wastage of memory of the program, these execution time benefits seems small.  In such cases, use of normal function is recommended.  
          An inline function definition is similar to the normal function except that the keyword inline is specified before the function's return type in the function definition.  The syntax for defining inline function is 
     inline ret_type func_name (parameter_list) 
     {
     function body
     }
          The inline function should always be defined before the main () function so that inline function definition must be visible for the compiler to be able to inline a function at the point  of the call.  So, there is no need to specify the function prototype (or function declaration). 
Program to show use of inline function which calculates the maximum of two numbers


Explanation: In above program, the inline function max () is used to calculate the maximum of two integer numbers.  On compilation, the statement a =max (6,8);  invokes the inline function max () and the body of inline function is expanded inline at the point of call i.e.  a = 6> 8?6:8. Similarly it expands the other function call statement.  
          Here, the statements written in the inline function max () are inserted at each place without any transfer of control between the calling and the called function.  Finally, when we execute the program, we get the desired result. 
 int main()                                 int main()              
{                                                {
...............                                    ...............
a=max(6,8);      Expression       a=6>8?6:8;
...............                                   ...............
b=max(m,n);                              b=m>n?m:n;
...............                                   ...............
}                                                 }

 An Inline specification in the function is only a request and not a command to the compiler.  However, the compiler may ignore the request and treat the inline function as a normal function under the following situations.  
  • If the function body contains a large number of statements like a switch or goto or looping statements such as while, do - while, for etc.  
  • If function contains static variables.  
  • If the function is recursive because it cannot be expanded completely at the point of its call           

ADVANTAGES OF INLINE FUNCTIONS 

1. Its use reduces the execution time because the body of the inline function is inserted at each point of inline function call and hence there is no  function call overheads as in case of normal functions.  
2. They provide strong type checking as compared to macros where no type checking is performed at all.  
3. Improves the program readability and modularity as it possess the features of functions with reduced execution time.  

DISADVANTAGES OF INLINE FUNCTIONS 

1. All functions that uses the inline function must be recompiled if any changes are made to the inline function.  
2. Although the inline function reduces the execution time, it increases the size of the executable file as multiple copies of the inline function's body are inserted in the program.  So it is always recommended to use inline functions for small frequently used functions.  
3. The definition of inline function should appear before the function call.  


No comments

Powered by Blogger.