Inline Functions in C++

7:23 PM 0 Comments

Inline Functions in C++

Inline function is one of the important feature of C++.
Inline function is a function that is expanded in line when it is called.
When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call.
This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small.
it work's like macros
The syntax for defining the function inline is:

inline return-type function-name(parameters)
{
    // function code
}

NOTE::
inlining is only a request to the compiler, not a command.
Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop.
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return statement doesn’t exist in function body.
5) If a function contains switch or goto statement.


 keep in mind that inline functions are the valuable feature of C++.
 An appropriate use of inline function can provide performance enhancement but if inline functions are used
 arbitrarily then they can’t provide better result. In other words don’t expect better performance of program. Don’t make every function inline.
 It is better to keep inline functions as small as possible.


in next post i will tell you why inline function is needed.................,

Unknown

Live as if you were to die tomorrow. Learn as if you were to live forever.

0 comments: