DEV Community

Vishal Yadav
Vishal Yadav

Posted on

Inline function in cpp

Inline function :

  • whenever a function is called it tasks a lot of extra time in executing a series of instruction for task such as
  1. Jumping to the function

2.Saving Register

3.pushing argument into the stack and returning to the calling function

  • Above task is executed either the function is small or big

  • So whenever the function is small these task become overhead one solution is use macro and second is that inline function concept

  • inline when it is invoke. that is the compiler replaces the function call with the corresponding function code.

To make a function Inline

#include <iostream>
using namespace std;
inline int square(int a)
{
    return a*a;
}
int main()
{
    cout << "The square of 2 is: " <<square(2)<<;
    return 0;
} 
Enter fullscreen mode Exit fullscreen mode

Reminder:

  • Remember that the inline keyword sends the request, not a command, to the compiler

  • The compiler may ignore this request if the function definition is too long & too complicated.

  • then compiler will treat the function as a normal function.

  • Thanks For Reading

  • Happy Coding

Top comments (0)