Explain Inline functions with Example Advantages and Disadvantages

Explain Inline functions with Program, Inline Function Advantages and Disadvantages



Inline functions: -


Function execution involves the overhead of jumping to and from the calling statement. Trading of this overhead in execution time is considerably large whenever a function is small. Hence in such case inline functions are used. Inline functions are those whose function body is inserted in place of the function call statement during the compilations process. An inline function definition is similar to an ordinary function, except that the keyword inline precedes the function definition.
•    By default every function created in C++ is Inline.
The syntax for defining an inline function is as follows:
            inline function- header ( )
            {    Body of the function;
            }
eg.   inline float getdata (int a, float b)
        {    return (a+b);    }
        void main ()
        {    int a= 35;    float b= 18.5;
        cout <<“The sum is “<<getdata (a,b);    }

The inline functions are similar to macros in ‘c’ language. But the major drawback with macros is that the error checking does not occur during compilation.
The following are the situations where inline functions may not work properly


1.    Functions returning values, having a loop, a switch or a go to exists
2.    If the function contains static variables.
3.    If inline functions are recursive.


Disadvantages: -       
1.    In line functions may require more memory in the program as they are basically expanded while compiling the program.
2.    The speed benefits of inline functions diminish as the functions grows in size.
The inline key word sends a request but not a command to the compiler. Hence the compiler can ignore the request if the function definition is too long.

Labels: