answersLogoWhite

0


Best Answer

Inline functions effectively remove the function call altogether. However, just because a function is declared inline is no guarantee it will be inline expanded. it is merely a hint to the compiler that the function should considered for inline expansion, nothing more.

To understand how inline expansion helps, consider the following code:

void print(int x) {

std::cout << x << std::endl; }

int main()

{

int x=5;

print(x);

x*=3;

print(x);

print(++x);

return(0);

}

If the print() function could be inline expanded, it would be the same as if you'd written the following:

int main()

{

int x=5;

std::cout << x << std::endl;

x*=3;

std::cout << x << std::endl;

std::cout << ++x << std::endl;

return(0);

}

Our code may be a little larger, but it will execute that much faster because we've completely eliminated all three function calls. But while we could expand functions inline manually, as shown above, it makes no sense to do so. In a larger program where the function might be called several dozen times, it becomes that much harder to manage the inline code because there are so many instances of it. By keeping the function and declaring it inline, we can easily modify the function's behaviour because the code is all in one place. More fundamentally, however, if we manually expand functions inline, our code becomes that much larger, and performance is directly related to code size.

By giving the compiler hints as to which functions should be inline expanded, we allow the compiler to decide which is better in each case. The compiler uses complex algorithms to determine the best compromise between code size and performance. A function that is called many times but has little actual code is an ideal candidate for inline expansion, but then so is any function, regardless of complexity, that is called just the once. In the latter case the function may not be absolutely necessary, but functions can help make the code that uses them that little bit more readable and easier to understand, with much less need for commentary.

Even recursive functions can be inline expanded, but usually there is a limit to the depth of recursion (16 being a common limit). This can greatly increase the performance of the function at the expense of code size, which may affect other areas of the program. However, it is not something that should concern the programmer. The compiler is a better judge of what should be expanded and what should not. Even so, some compilers also include a non-standard force_inline flag that can override the compiler's decision, but this is best used sparingly, and only when the programmer is absolutely sure the performance gained will not cause problems elsewhere due to the increased code size.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the Advantages of in line function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the building function in c plus plus?

There is no such term as "building function" in C++.


Advantages of manipulator in c plus plus?

theriyadhu


What type of function is y equals 2x plus 8?

Y = 2X + 8 is a linear function of the form, Y = mX + c. A line.


A c plus plus statement that invokes a function is known as?

...a function call.


When will you make a function inline in c plus plus?

yes,we can make function inline


What is the only function all C plus plus programs must contain?

Every C plus plus program that is a main program must have the function 'main'.


In C plus plus when a function is executing what happens when the end of the function is reached?

Control is returning to the caller of the function.


What do you call an object function in c plus plus?

method


What is a main function in c plus plus?

It is the first function that gets called when the program is executed.


Can a Horizontal line represent a function?

Yes. It is the function f(x) = c where c is a constant.


What is the function y equals ax2 plus bx plus c?

It is a quadratic function which represents a parabola.


What are the basics of main function in c and c plus plus?

The main function is the entry point of your application. Its primary purpose is to process command-line switches (if any) and to invoke the appropriate functions. For trivial applications the main function may be the only function, however separating blocks of code into re-usable functions make code much easier to read.