Tuesday, 11 September 2012

inline functions

When a same task is to done for many times, the function is used to save memory space. But, the use of function not only saves memory space but also adds extra overheads. One function call has to pass many steps like jumping to the function, saving registers, pushing arguments into the stack and returning to the calling function. A lot of time is taken by these instructions. When function is small, a substantial percentage of execution time may be spent in such overheads


 This problem is eliminated by inline function in C++. An inline function is a function that is expanded in line when it is invoked. The compiler replaces the function call with the corresponding function code. A function is made inline by adding prefix inline to the function definition. The syntax is
        Inline  return_type  function_name(argument_list)
             {
             }


When inline function may not work?
Ø  For large functions
Ø  For functions containing static variables
Ø  For recursive functions
Ø  For function containing a loop, a switch or a goto.

No comments:

Post a Comment