C++ - Inline Function and It's Use

C++ - Inline Function and It's Use Inline function is used to minimize the time. It is used if there are 2 or 3 statements only. Function is copied to the main function after calling function. To use inline a function, place the keyword inline before the function name and define the function before any calls are made to the function.

An inline function is used to minimize the time. It is used if there are 2 or 3 statements only. The function is copied to the main function after calling the function.

To use inline a function, place the keyword inline before the function name and define the function before any calls are made to the function.

The compiler can ignore the inline qualifier in case defined function is more than a line. A function definition in a class definition is an inline function definition, even without the use of the inline specifier.

Example,

#include<iostream> using namespace std; inline int subtraction (int a, int b) { return (a-b); } int main(void){ cout<<subtraction(5,4); cout<<subtraction(6,7); cout<<subtraction(9,4); return(0); }
Drawbacks: Situations where inline function doesn't work are,
  1. If function contains static variable. 
  2. If inline function is recursive. 
  3. For function returning values, if loop, a switch or got exists. 
  4. For function non-returning values it return statements exists.

You May Also Like...