22 Nov 2019

  • November 22, 2019
  • Amitraj
Inline Function in C++

-> 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 inline when it is invoked.

-> That is, the compiler replaces the function call with the corresponding function code (something similar to Macros in C).

-> The inline function are defined as follows:
       
             inline  function  header
                 {
                        function body
                  }




// C++ program to demonstrate inline function.


#include<iostream>
using namespace std;
inline int mul (int a,int b)
{
        return a*b;
}
int main()
{
        cout<<mul (5,10);
        return 0;
}

OUTPUT:-

50


*Rules for inline function:-


inlining is only a request to the compiler not a command. compiler can ignore the request for inlining. compiler may not perform inlining in such situations like:

1.) if a function contains a loop. (for,while,do-while)
2.) if a function is recursive.
3.) if a function contains static variables.
4.) if a function contains switch or goto statement.
5.) if a function return type is other than void and the return statement doesn't exist in function body.
6.) inline function can be used when the member function contains few statement. 

Translate

Popular Posts