Inline functions

Generally when a function is called, the control is transferred to that function, along with some values if necessary, and after execution, the control is transferred back to the calling function. Obviously it saves memory space but certainly increases execution time. To reduce this execution time, C++ language provides inline functions.

The syntax of declaring inline functions is:

              inline function_name(list_of_arguments)
                     // Function body

A function is made “inline” by using the keyword inline before the function’s return type of the function definition. Whenever we call inline function, the C++ compiler places the entire code of inline function directly inside the code of the calling function. In other words we can say that inline functions are just like preprocessor macros because the compiler substitutes the entire function body for each inline function call.

If the function code is small, straight-line or when there are relatively few calls to it then use inline function otherwise uses the normal function.
Remember that when we define the function inline, it is up to the C++ compiler whether to insert the code of inline function or not to insert. 

No comments:

Post a Comment