answersLogoWhite

0


Best Answer

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say:

int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro:

#define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like:

max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like:

max(a++, b++) An alternative in C++ is to use inline functions:

inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.

Suppose that you wish to write a function in C to compute the maximum of two numbers. One way would be to say:

int max(int a, int b) { return (a > b ? a : b); } But calling a frequently-used function can be a bit slow, and so you instead use a macro:

#define max(a, b) ((a) > (b) ? (a) : (b)) The extra parentheses are required to handle cases like:

max(a = b, c = d) This approach can work pretty well. But it is error-prone due to the extra parentheses and also because of side effects like:

max(a++, b++) An alternative in C++ is to use inline functions:

inline int max(int a, int b) { return (a > b ? a : b); } Such a function is written just like a regular C or C++ function. But it IS a function and not simply a macro; macros don't really obey the rules of C++ and therefore can introduce problems. Note also that one could use C++ templates to write this function, with the argument types generalized to any numerical type.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Advantage: Macros and Inline functions are efficient than calling a normal function. The times spend in calling the function is saved in case of macros and inline functions as these are included directly into the code.

Disadvantage: Macros and inline functions increased the size of executable code.

Difference in inline functions and macro

1) Macro is expanded by preprocessor and inline function are expanded by compiler.

2) Expressions passed as arguments to inline functions are evaluated only once while _expression passed as argument to inline functions are evaluated more than once.

More over inline functions are used to overcome the overhead of function calls. Macros are used to maintain the readbility and easy maintainence of the code.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

it only increase performance for the code that the compiler output but with today's optimized compilers, fast CPUs, huge memory etc (not like in the 1980< where memory was scarce and everything had to fit in 100KB of memory

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Inline functions are placed directly into the executable code instead of being called through a function pointer. Inline functions trade executable code size (which will be larger) for code execution speed (which will be faster). The decision to inline lies with the number of times the function is used in code and its size, and the number of times it is called during the course of a program. For example, a function that grabs a single pixel from a screen buffer is an excellent candidate for inline compilation, since it will likely be called many millions of times per second, with a total savings of approximately 90 cycles per call (depending on architecture), which can be the difference between 10 frames per second and 60 frames per second.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Inline functions should be used when either the code that the function will contain is minimal (maybe one line or two), or where the function will be called very often.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is inline function what are the advantages of using inline function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What happens if recursion function is declared inline?

An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.


When will you make inline function?

Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.


What are advantages of using the sum function?

It gives you the sum of two or more numbers.


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 is inline function in C Can you make inline function recursive or not If make can complier will compile that code?

The inline attribute is a C++ attribute, not a C attribute. Inline specifies that the function is to be expanded in place at the point of call instead of being called as a function. This means there will be one copy of the function for each call. This costs executable code, but can save execution time because the call setup and return time is avoided. Some functions cannot be inlined, and inline is really only a hint to the compiler. As far as recursive inlined functions, that depends on the implementation. The Microsoft implementation will not inline recursive functions unless they have a #pragma inline depth(n) line that specifies the maximum recusion depth the function will have. Consult your specific compiler documentation for the inline attribute for your specific implementation details.

Related questions

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

yes,we can make function inline


What happens if recursion function is declared inline?

An inline function replaces the call to the function by the body of the function, thus reducing the overhead of saving the context in stack. This is good for functions which are small in size and called occasionally. A recursive function calls an instance of itself and thus can be a deeply nested. Different compilers handle this differently. Some will inline it up to a certain depth and then call a non-inlined instance for further recursion; others will not inline the function at all and generate a normal function call.


Is inline function a command?

No, functions (inline or other) aren't commands.


Difference between normal function inline function?

gffg


What are the advantages in using a rule for a function rather than listing function values in a table?

If the domain is infinite, it is not possible to list the function.


When will you make inline function?

Trivial functions, such as member variable accessors that simply return a member's value, are prime candidates for inline expansion. However trivial non-member functions can also be inline expanded, as can any non-trivial function that is rarely called.Member functions defined in the body of the class declaration are implicitly declared inline. However, whether a function is explicitly declared inline or not, the compiler is free to ignore any inline request, such as when the inline expansion of a non-trivial function would adversely compromise code size, for instance.Note that inline expansion replaces the call to a function with a modified version of the function's body within the calling functions -- just as if you'd duplicated the code yourself, rather than creating a separate function -- which removes the overhead of making a function call.The only way to force a function inline is to manually write the expanded code yourself. But if the code appears in several places, maintenance of the code will be compromised.If there's ever any doubt, declare it inline and let the compiler decide. It's in a far better position to determine if it should be inline expanded or not.


What is the difference between an outline and inline?

For the inline functions compiler just copies the function code in that place and when the size is too big it treats that function as ordinary function.


What is the advantages of a inline engine?

Joel stirland facebook me


Inline functions in c plus plus with prgram example?

#include&lt;iostream&gt; using namespace std; inline int max(int a,int b) { return (a&gt;b)?a:b; } int main() { int i1=3,i2=5; cout&lt;&lt;endl&lt;&lt;"Inline function says max is "&lt;&lt;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.*/


What are advantages of using the sum function?

It gives you the sum of two or more numbers.


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.


Which keyword is used to make function call faster in c?

Inline Function