Thursday 15 March 2012

INLINE FUNCTIONS



DEFINITION:
An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. Instead of transferring control to and from the function code segment, a modified copy of the function body may be substituted directly for the function call. In this way, the performance overhead of a function call is avoided.

A function is declared inline by using the inline function specifier or by defining a member function within a class or structure definition. The inline specifier is only a suggestion to the compiler that an inline expansion can be performed; the compiler is free to ignore the suggestion.
The following code fragment shows an inline function definition:
inline int add(int i, int j)
 {
      return i + j;
 }
The use of the inline specifier does not change the meaning of the function. However, the inline expansion of a function may not preserve the order of evaluation of the actual arguments. Inline expansion also does not change the linkage of a function: the linkage is external by default.
         In C++, both member and nonmember functions can be inlined. Member functions that are implemented inside the body of a class declaration are implicitly declared inline. Constructors, copy constructors, assignment operators, and destructors that are created by the compiler are also implicitly declared inline. An inline function that the compiler does not inline is treated similarly to an ordinary function: only a single copy of the function exists, regardless of the number of translation units in which it is defined.
        In C, any function with internal linkage can be inlined, but a function with external linkage is subject to restriction. . The restrictions are as follows:
If the inline keyword is used in the function declaration, then the function definition must appear in the same translation unit.
An inline definition of a function is one in which all of the file-scope declarations for it in the same translation unit include the inline specifier withoutextern.
An inline definition does not provide an external definition for the function: an external definition may appear in another translation unit. The inline definition serves as an alternative to the external definition when called from within the same translation unit. 
        In the Ada programming language, there exists a pragma for inline functions. Most other languages, including Java and functional languages, do not provide language constructs for inline functions, but often do perform aggressive inline expansion.

ADVANTAGES:
  • It saves the time required to execute function calls.
  • Small inline functions, perhaps three lines or less, create less code than the equivalent function call because the compiler doesn't generate code to handle arguments and a return value.
  • Functions generated inline are subject to code optimizations not available to normal functions because the compiler does not perform inter procedural optimizations.
DISADVANTAGES:
  •  Inlining can increase the size of your executable program significantly leading to more number of page faults bringing down program performance.
  •  If used in header file, it will make your header file size large and may also make it unreadable.
DIFFERENCES BETWEEN INLINE FUNCTIONS AND MACROS:

  •  Macro is expanded by preprocessor and inline function are expanded by compiler.
  •  Expressions passed as arguments to inline functions are evaluated only once while expression passed as argument to inline functions are evaluated more than once. 
  • Inline functions are used to overcome the overhead of function calls. Macros are used to maintain the readability and easy maintainence of the code.
  • Debugging is tough in macros (because they refer to the expanded code, rather than the code the programmer typed) whereas debugging is easy in inline functions.
  • Macro invocations do not perform type checking, or even check that arguments are well-formed, whereas function calls usually do.
  •  A macro cannot return anything which is not the result of the last expression invoked inside it but in inline functions we can return any value by using keyword return();

No comments:

Post a Comment

Note: only a member of this blog may post a comment.

Labels