One of the prime factors behind using function is that code duplication in program is avoided and memory space is saved. When a function is defined and invoked, one set of instruction is created in the memory of the system. At each call, the control passes to the subroutine at a specified address in the memory. The CPU stores the memory address of instruction following the function calls in to the stack and also pushes the arguments in the stack area when call is made to the function. The compiler runs the function, stores the return values in a memory location or register, and when execution completes, the control returns to the calling function. After this, execution resumes immediately to the next line following the function call statement. If the same function is called several times, each time the control is passed to the function, that is the compiler uses the same set of instructions at each call. Due to this passing of control in between caller and callee functions, execution speed of program decreases. By preventing the repetitive calls in a program, execution speed can be increased.
C++ provides a mechanism called inline function. When a function is declared as inline, the compiler copies the code of the function in the calling function, that is function body is inserted in place of function call during compilation. Passing of control between caller and callee functions is avoided. If the function is very large, in such a case inline function is not used because the compiler copies the contents in the called function that reduces the program execution speed. The inline function is mostly useful when calling function is small. It is advisable to use the inline function for only small functions.
Inline mechanism increases execution and performance in terms of speed. The overhead of repetitive function calls and returning values are removed. On the other hand, the program using inline functions needs more memory space, since the inline function are copied at every point where the function is invoked.
For defining an inline function, the inline keyword is preceded by the function name. The inline functions are declared as follows:
The above example can be executed as follows:
j = square(2.5);
k = square(1.1 + 1.4);
After the execution of above statements, the values of j and k will be 6.25. The inline keyword just makes an appeal to the compiler. The compiler may neglect this request if the function defined is too big in size or too convoluted. In such a case the function is treated as normal function.
Following are few situations where inline functions may not work:
- The function should not be recursive.
- Function should not contain static variables.
- Function containing control structure statements such as switch, if, for loop, etc.
- The function main() cannot be used as inline.
The inline functions are similar to macros of C. The main limitation of macros is that they are not functions, and errors are not checked at the time of compilation. The function offers better type testing and does not contain limitations as present in macros. Consider the following example:
7.16 Write a program to calculate square using inline functions and macros.
Explanation: In the above program, the function square() is declared as an inline function. The macro square() expanded into r = ++v * ++v. The variable p is incremented only once but in macros expansion is incremented twice. Therefore, it gives wrong result.
7.17 Write a program to define an inline function and obtain the result.
Explanation: In the above program, the function sum() calculates the sum of given integers. The function is defined as inline. The compiler copies the code of the function in the calling function and executes it.
7.18 Write a program to define function cube() as inline for calculating cube.
Explanation: In the above program, the function cube() is declared as inline. The function cube() calculates cube of passed number. The function is declared as inline, that is the statements of function cube() are inserted at the point of function call.