You are here:

C++/Inline function in c++

Advertisement


Question
where we should use inline function and why we use inline function in c++ !

Answer
Hi dear Sunil !

 There is a major difference between the Normal function in C/C++ and an inline function in C++

eg Lets have a normal function

 void foo()
 {

   // some definitions

 }

void main()
{
  foo(); // A typical call function call
}

during your programme execution if the control encounters the function call it will save the current line of execution and the status of execution up to the line of code preceding the function call into a stack / registers , then it will jump to the function where the definition was actually written in the object module ( make sure this will disturb the normal flow of execution in terms of addressing )

After the function call has been accomplished it will return the main() by popping out address earlier it pushed onto the stack before the function call. this push and pop operation will require some time ( say a fraction of time favoring performance issue ).if its a big function (say > 10 lines ) the pay load will be balanced, and if its just one or two lined function cost is high in terms of execution time.

To overcome this problem C++ introduced inline functions. if the control encounters the inline function call it won't jump to the function definition as on normal function call , instead the function call will be replaced with the code ( expanded inline ) as macros in C Language

eg:

inline  void foo()
{
 cout<<"inline function call";
}

void main()
{
  foo();
}

during execution;

void main()
{
 cout<<"inline function call"; // note here the function call will be replaced with definition
}

Note:
Not all the function will be treated as inline functions in C++ even though if you declare a function as inline , the choice is purely done by the C++ compiler.

Hope this would help u.

Thanks and Regards!
Prince M. Premnath.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.