Tuesday, February 26, 2008

inline function, a move over #define

One the feature of C++ that I find very cool is inline function. It actually kills all those crazy interview questions which are asked just to confuse you over brackets. I dont remember ever using the complicated #define in C to anything more than example programs. Crazy part of inline funtion is that though u code almost as a normal function call but it gives the advantages of writing code right where u called the function. No more making sure that you have brackets in place and how its going to expand at all the places, how its going to take in the parameters passed to it, how will the return value be determined. Dump all that. How to stuff is here

void foo()
  int a,b, c;
  c = bar(a,b);

inline int bar(int a, int b)
  //Just do something here
   return something;

So you have the function foo calling function bar, but as bar is inline code for it would be placed right where it was called in foo. So we see how inline function has simplified life in C++. Any question of yours in this regard would be appreciated and I would try my best to answer it.


Anonymous said...

Very nice insights into working of virtual and inline, however I have a doubt. What is the difference? If control going to the function and returning, and if the code is put at the caller position. What benefit do we get by using inline keyword?
Can you please give some simple and application specific example?
Thanks. Keep writing about C++.

100rabh™ said...

If you look into the assembly code for it a normal and an inline function, you will realise that there is a return code with a normal function. A return call in any processor takes more time than writing that code there itself, which is kind of obvious :-)

These inline are very useful in case of time critical applications.

and I would continue this blog as and when I get some breathing space from my job :-)