answersLogoWhite

0


Best Answer

The advantage of an inline function is that it eliminates the function calls completely, thus improving the performance wherever those calls appear. The disadvantage is that it increases code size, which can be detrimental to performance. For this reason, declaring a function for inline expansion is merely a hint to the compiler. If the increased code size would be detrimental, the compiler is free to ignore the inline declaration and retain the function call instead.

While the programmer is free to manually expand their own functions, this only serves to increase maintenance should the function ever need to be changed, and could lead to errors should those changes not be propagated correctly. The advantage of having a function, even if it is only ever called once (or from within a loop), is to simplify your code and make it easier to read and maintain. If the function is simple, or is called seldom, then it is a good candidate for expansion, but its almost always better to let the compiler decide which functions should be inline expanded.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Advantage and disadvantage of inline function in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is outline function in c plus plus language?

Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.


What are disadvantages of virtual functions in c plus plus?

Virtual functions have many advantages, but here are a couple of their disadvantages:slower -- The function call takes slightly longer due to the virtual mechanism,and it also makes it more difficult for the compiler to optimize because it doesn't know exactly which function is going to be called at compile time.harder to debug -- In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from. Or, to figure out why a function isn't being called if someone overrode it with a new virtual function.


What is an example program in c plus plus to square a number using the concept of an inline function?

... double squareOf_Number(double Number){return (Number*Number);}...int main(){...double Number = 0;...printf("Enter a number: ");cin >> Number;...printf("Square of %f is %f\n", Number, squareOf_Number(Number));...}Or you can include #include and use the function pow(double a, double b) which returns a^b.


C plus plus code to calculate a right angle?

Right angles are always 90 degrees. There is no need to calculate this; an angle is either 90 degrees or it is not. The following inline function is all you really need: inline const bool IsRightAngle(double angle) { return(angle==90.0); }


Is a function call a form of branching in C plus plus?

No. A branch is akin to a goto statement in procedural programming. The code branches off to a new code segment, never to return. A function call is akin to a subroutine in structured programming. When the subroutine is finished, control is returned to the instruction immediately following the function call, just as if the function's code were inline expanded at the call site.

Related questions

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

yes,we can make function inline


What is outline function in c plus plus language?

Outline is the opposite of inline. An inline expanded function is any function or class method where the declaration also provides the definition (the implementation). This is known as implicit inline expansion. Where the definition is kept separate from the declaration, you may use the inline keyword to specifiy that the function should be inline. This is known as explicit inline expansion. Inline expanded functions (whether implied or explicit) does NOT mean the function will in fact be inline expanded. It is merely a suggestion to the compiler. If the compiler's optimisers determine that there is no advantage to be gained by inline expanding a particular function, then that function becomes an outline function. Inline expansion simply means that the body of the function is inserted in place of the function call. Function calls are expensive in terms of memory and performance, so by eliminating the function call completely, your code performs faster and uses less memory. However, functions that are called many times throughout your code will result in a much larger code size, and large code runs slower than small code. Thus the benefit of eliminating a function call has to be weighed against the increased code size. Although some compilers do allow you to override the compiler's inline expansion optimisers, this is strictly non-standard. The best judge of what to expand and what not to expand is best left in the hands of the compiler, and indiscriminate use of the inline keyword should generally be avoided.


What are the 5 advantage and 5 disadvantage of hoarding poster?

five plus five = ten


Inline functions in c plus plus with prgram example?

#include<iostream> using namespace std; inline int max(int a,int b) { return (a>b)?a:b; } int main() { int i1=3,i2=5; cout<<endl<<"Inline function says max is "<<max(i1,i2); return 0; } /* Usually when a function is called, the compiler goes to the particular piece of code and executes it. But in the case of inline functions, the code from the body of the function is effectively pasted at the point of call. inline functions are used when the body of the function is only a line or so.*/


Automatic inline in c plus plus?

The C++ compiler will implicitly (automatically) mark functions for inline expansion whenever you define a function within its own declaration. If functions are declared and defined separately (even in the same file) then they are not implicitly marked for inline expansion. To enable inline expansion for these definitions, you must explicitly mark the definition (not the declaration).


The advantages and disadvantages of full cost plus pricing?

The advantage of full cost plus pricing is the higher return on investment. The disadvantage of full cost-plus pricing is lower demand for the products.


What are disadvantages of virtual functions in c plus plus?

Virtual functions have many advantages, but here are a couple of their disadvantages:slower -- The function call takes slightly longer due to the virtual mechanism,and it also makes it more difficult for the compiler to optimize because it doesn't know exactly which function is going to be called at compile time.harder to debug -- In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from. Or, to figure out why a function isn't being called if someone overrode it with a new virtual function.


What is an example program in c plus plus to square a number using the concept of an inline function?

... double squareOf_Number(double Number){return (Number*Number);}...int main(){...double Number = 0;...printf("Enter a number: ");cin >> Number;...printf("Square of %f is %f\n", Number, squareOf_Number(Number));...}Or you can include #include and use the function pow(double a, double b) which returns a^b.


C plus plus code to calculate a right angle?

Right angles are always 90 degrees. There is no need to calculate this; an angle is either 90 degrees or it is not. The following inline function is all you really need: inline const bool IsRightAngle(double angle) { return(angle==90.0); }


Is a function call a form of branching in C plus plus?

No. A branch is akin to a goto statement in procedural programming. The code branches off to a new code segment, never to return. A function call is akin to a subroutine in structured programming. When the subroutine is finished, control is returned to the instruction immediately following the function call, just as if the function's code were inline expanded at the call site.


What are the rules for inline functions in c plus plus?

When you mark function as inline compiler puts the whole body of function in those places it is called, similar idea as in macros. If you do not mark function as inlinecompiler inside still decides which functions should be inline and which not. Inline function is less performance costly especially if function is called very often. Why it is lest performance costly? Because to invoke function you need to prepare parameters, put them to stack, make jump and etc. and all those steps are eliminated if function is inline.Example (very basic):inline int sum(int a, int b) {return a + b;}int c, d;c = sum(2, 3); /* compiler will change to 2 + 3 */d = sum(2, 5); /* this one will be changed to 2 + 5 */Full inline functions are allowed in ANSI/ISO C99.


What is a member function definition inside a class and outside of a class and how are they declared in C plus plus?

Member functions must always be declared inside a class declaration, however they may be defined either inside or outside of the class. A definition is simply the implementation of a function, the code that is executed when the function is called.When a function is defined inside a class declaration then it is implicitly inline expanded. When it is defined outside of a class declaration, it is not inline expanded but you may explicitly declare it to be inline expanded, if desired. Note that inline expansion should only be utilised when the function has but a few simple statements, preferably just one or two statements at most.The following example demonstrates the definition of a typical class accessor (a getter) defined within a class declaration (where inline expansion is implied and desired):class A {public: int get_data()const{return(m_data);}private: int m_data;};The following example shows the same function defined outside of the class. This time the function will not be inline expanded.class A {public: int get_data()const;private: int m_data;};int A::get_data()const{return(m_data);}Note that the definition may appear in a different file. Generally, classes are designed with a header file and a source file, where the header contains the declarations and the source contains the definition. The source file must include the header file.Since it is often desirable to inline expand simple class accessors that merely return values, the inline keyword can be used when the definition is external to the class declaration, like so:class A {public: inline int get_data()const;private: int m_data;};int A::get_data()const{return(m_data);}Note that declaring a function inline (implicitly or explicitly) is no guarantee that it will actually be inline expanded, you are merely signalling to the compiler that the function is a candidate for expansion. The compiler is still free to veto the promotion if its inline optimisers deem that such an expansion would compromise performance due to the increased code size that inline expansion incurs. Functions that are called in only one place in your code, regardless of how complex they are, are generally good candidates for expansion. Although you could manually inline expand such functions, if the function call makes your calling code easier to read and maintain, then it's better to retain the function in your code.Note also that while some compilers allow you to force an inline expansion (such as Microsoft's __forceinline keyword), effectively bypassing the compiler's optimisers, this should be done sparingly as the increased code size can easily outweigh any performance gained by removing the function call. Also note that some functions cannot be inline expanded, even by force. In particular, the compiler cannot inline expand any of the following:a function with a variable argument list.a recursive function (see * note below).a function compiled for debug builds.where the function and the caller use different types of exception handling.a function that uses inline assembly (often dependant upon compiler flags).a virtual function that is called virtually rather than directly.indirect calls via function pointers.* Some recursive functions can be inline expanded up to a predetermined depth, usually 16 calls at most (thereafter, the calls are treated as calls to new instances of the function). The predetermined depth generally cannot be increased, but it can typically be reduced with a pragma.For more specific information on inline expansion within your compiler or IDE, consult the compiler's documentation regarding the inline keyword.