4

Possible Duplicate:
Benefits of inline functions in C++?

What is the difference between

#include <iostream>
using namespace std;
int exforsys(int);
void main( )
{
        int x;
        cout << "n Enter the Input Value: ";
        cin>>x;
        cout << "n The Output is: " << exforsys(x);
}

int exforsys(int x1)
{
        return 5*x1;
}

and

#include <iostream>
using namespace std;
int exforsys(int);
void main( )
{
        int x;
        cout << "n Enter the Input Value: ";
        cin>>x;
        cout << "n The Output is: " << exforsys(x);
}

inline int exforsys(int x1)
{
        return 5*x1;
}

these two definition is work same for a code I guess, then what's the advantage of using the inline function definition?

Community
  • 1
  • 1
user1532043
  • 859
  • 2
  • 15
  • 26
  • http://stackoverflow.com/questions/3650471/c-is-the-inline-keyword-worth-it might be useful – PeterJ Dec 26 '12 at 05:34
  • Also, Wikipedia's [Inline function](http://en.wikipedia.org/wiki/Inline_function) gives a good explanation. – Nocturno Dec 26 '12 at 05:35

6 Answers6

11

The inline keyword suggests to the compiler that the function be inlined. Normally, when a function is called, the current contents of the registers are pushed (copied) to memory. Once the function returns, they are popped (copied back).

This takes a little time, though normally so little that whatever the function does dwarfs the function call overhead. Occasionally when a very small function is called thousands of times per second in a tight loop, the combined function call overhead of all those function calls can add up. In these cases, a programmer can suggest to the compiler that, rather than call the function in that loop, that the contents of the function be put in the loop directly. This avoids the overhead.

Some compilers, notably Microsoft Visual C++, ignore the inline keyword. Microsoft believes their optimizer is smart enough to know when it should inline a function. For those cases when you really want a function to be inlined, Microsoft and other vendors sometimes provide a proprietary, "no, I really mean it!" keyword. In the case if Visual C++, it's __forceinline if I remember right. Even this, however, still can be ignored if the optimizer feels very strongly that inlining that function is a bad idea.

Charles Burns
  • 10,310
  • 7
  • 64
  • 81
6

Since compilers got smart enough to decide which functions would benefit from being inlined and which wouldn't, inline only real effect is to change the function linkage. By default, inline functions have external linkage.

The inline keyword is just a suggestion to the compiler. The compiler could decide to inline functions not declared inline, or to not inline functions declared inline.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • 1
    `inline` is just a suggestion to compiler is not true & little misleading.There are two implications of marking a function `inline`. 1. Substitution of function definition inline to where the function call was made & 2. Certain guarantees w.r.t One Definition Rule. An compiler may or may not perform `#1` but it has to abide to `#2`. So `inline` is not just a suggestion.There are some rules which will be applied once function is marked `inline`.Simply said, `inline` is not only related to function definition inlining. – Alok Save Dec 26 '12 at 05:57
  • @Alok Save: `#2` is not a consequence of `inline`, but of _external linkage_. If a function where declared `static inline` then `#2` does no longer apply... – K-ballo Dec 26 '12 at 05:58
  • That doesn't imply `inline` on a non-static function is only a suggestion. – Alok Save Dec 26 '12 at 06:03
  • @Alok Save: I see your point, I just wanted to make sure that future readers don't get confused – K-ballo Dec 26 '12 at 06:07
0

Well, code for inline functions are placed at its usage during compile time. Hence, its a bit fast in execution and source code length remain small.

Aditya Jain
  • 1,077
  • 1
  • 12
  • 25
0

When you call an inline function, instead of transferring the execution control to this function, the preprocessor replaces all such function calls with the actual function code. In C++, when you define a function inside the class, it's inline by default provided that it doesn't have loop statements.

ragingasiancoder
  • 616
  • 6
  • 17
Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
  • The preprocessor operates in this case when a define is used. I believe it's the compiler, not the preprocessor, that deals with inline. – Gustavo Litovsky Dec 26 '12 at 05:49
0

An inline function call is literally replaced with the inline function itself.

This explains it elegantly and simply. http://www.cprogramming.com/tutorial/lesson13.html

The wikipedia article is also helpful. http://en.wikipedia.org/wiki/Inline_function

DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119
0

From C++ language specification 7.1.2

A function declaration with an inline specifier declares an inline function. The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism.

It means the implementation can replace the method calls with the method body, thus avoiding the overhead which normally happens in normall method calling process. But it is not compulsory to do the inline replacing ,and it is implementation specific.

An implementation is not required to perform this inline substitution at the point of call; however, even if this inline substitution is omitted, the other rules for inline functions defined by 7.1.2 shall still be respected

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167