Pwnna
Pwnna

Reputation: 9538

When should I make a function inline?

Why should I do something like this:

inline double square (double x) { return x * x; }

instead of

double square (double x) { return x * x; }

Is there a difference?

Upvotes: 82

Views: 60536

Answers (6)

johannesMatevosyan
johannesMatevosyan

Reputation: 2228

From "Inline function" on Wikipedia:

Inline function is a function upon which the compiler has been requested to perform inline expansion.

In other words, the programmer has requested that the compiler insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. Compilers are not obligated to respect this request.

Upvotes: 6

Erik Nedwidek
Erik Nedwidek

Reputation: 6184

Yes there is a difference. https://isocpp.org/wiki/faq/inline-functions.

When you specify that a function is inline you are causing the compiler to put the code of the method in where ever it is being called.

void myfunc() {
  square(2);
}

is identical to

void myfunc() {
   2 * 2;
}

Calling a function is good for code clarity, but when that function is called the local state has to be pushed to the stack, a new local state is setup for the method, and when it is done the previous state needs to be popped. That is a lot of overhead.

Now if you up your optimization level, the compiler will make decisions like unrolling loops or inlining functions. The compiler is still free to ignore the inline statement.

Upvotes: 28

Gio Borje
Gio Borje

Reputation: 21134

inline works well with the concept of procedural abstraction:

inline double square (double x) { return x*x;}

int squareTwice(double x) {
    double first = square(x);
    double second = square(x);
    return first * second; 
}

The above is fundamentally similar to the following:

int squareTwice(double x) {
    double first = x*x;
    double second = x*x;
    return first * second; 
}

This happens because when the compiler inline-expands a function call, the function's code gets inserted into the caller's code stream; therefore, it may be easier to procedurally abstract the second example to the first example.

Procedural abstraction makes it possible to break up a routine into smaller subroutines that are much easier to read (although this can be a style choice).

Upvotes: 2

Ian Fleeton
Ian Fleeton

Reputation: 1157

The inline function, if the compiler complies, will include the inline function in the code in which it was called as if no function was called (as though you had put the logic in the calling function) and avoid the function call overhead.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 994649

The former (using inline) allows you to put that function in a header file, where it can be included in multiple source files. Using inline makes the identifier in file scope, much like declaring it static. Without using inline, you would get a multiple symbol definition error from the linker.

Of course, this is in addition to the hint to the compiler that the function should be compiled inline into where it is used (avoiding a function call overhead). The compiler is not required to act upon the inline hint.

Upvotes: 94

Ben Jackson
Ben Jackson

Reputation: 93900

On a modern compiler there is likely not much difference. It may be inlined without the inline and it may not be inlined with the inline.

Upvotes: 31

Related Questions