C++ Programming

C++ – Inline Function and Its Use

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 the 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>
usingnamespace std;
inline
intsubtraction (int a, int b)
{
return (a-b);
}
intmain(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 a function contains a static variable. 
  2. If the inline function is recursive. 
  3. For function returning values, if loop, a switch or got exists. 
  4. For function non-returning values it returns statements exists.

Related Articles

Back to top button

Adblock Detected

Please turn off your Adblocker to view your content.