DEV Community

Vishal Yadav
Vishal Yadav

Posted on

3 2

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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs