answersLogoWhite

0


Best Answer

The normal way a function works is that whenever your code encounters a call to the function, it jumps to the body of the function code. An inline function tells the compiler that it should actually copy over the code from a function body into all places where that function is called. In some cases this can cause a dramatic reduction in run time, but in others it causes nothing more than increasing the size of the produced executable.

Function overloading refers to the ability to have multiple functions with the same name, but different parameter types.

User Avatar

Wiki User

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

Wiki User

9y ago

An inline function is a function that is defined in a header file rather than in a translation unit. You would typically do this for small, simple functions that seldom change or whenever you define a template function (to ensure the definition is available to the compiler before any use of that template). Since a header file may be included in more than one translation unit, you must declare the function inline otherwise you will end up with multiple definitions of the same function, breaking the one definition rule (ODR). However, member functions that are defined within their class header declarations are implicitly inline.

Note that declaring a function inline does not mean the function will be inline expanded. That is a completely different thing altogether. Inline expansion is an optimisation feature of the compiler where a function's implementation is simple enough that calls to that function can be expanded -- thus eliminating the overhead of making an actual function call. This is achieved by replacing the function call with the actual code of the function, replacing the formal arguments with the actual arguments in the function call. However, inline expansion results in bigger code and the larger the code, the slower it will run. Thus the benefit of eliminating a costly function call has to be weighed up against the increased code size. The compiler's optimisers take care of this.

Since inline functions tend to be small and simple, they are good candidates for inline expansion, thus the implicit or explicit use of the inline keyword is often viewed as being a hint to the compiler that the function should be expanded. This may well be the case for some implementations, but this use of the inline keyword is not part of the standard. Some implementations even include a force_inline keyword (or similar) to ensure a function is always inline expanded, even when that would be detrimental to performance. This keyword is best avoided because a function may be used by several programs where inline expansion may or may not be appropriate. It is always best to let the compiler decide since it is in a far better position to decide which option is best.

Note that the compiler treats every function on its merits alone and will inline expand functions that may not be obvious candidates for expansion, including those defined in translation units which cannot be declared inline for that reason alone. This also includes recursive functions which are generally poor candidates for expansion. If the depth of recursion can be determined at compile time and the increased code size is favourable, then it will be expanded. In other cases it will be expanded up to a certain depth with any additional invocations falling back to a recursive function call. Again, this is not something that the programmer can easily determine and is something that is best left to the compiler to decide.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between inlinefunction and function overloading?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the difference between over-loading and over-riding in cpp?

Overloading and overriding do similar things but they are distinct. When you override a function, you are providing a new implementation of a function that was inherited from a base class. The signature of an override must be covariant with the base class signature. Overloading means to create several functions with the same name within the same namespace but with different signatures.


Example of fundamental difference between a polynomial function and an exponential function?

fundamental difference between a polynomial function and an exponential function?


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


What is the difference between compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


Difference between normal function inline function?

gffg


What is the difference between the function of an organ and the structure of an organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between the structure of an organ and the function the organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between the structure of an organ and function of an organ?

the difference is ,a structure is where it is placed and the function is what it daos


What is the difference between a formula and a function?

A function has no repeated x values


What is the difference between a function and a formula?

A function has no repeated x values


What is the difference between rational function and linear function?

t is the diffrence between a rational funcrion and a linerar and polynomial function


What is the difference between a function and Procedure?

Girraf