Functions in “C” and “c++” programs are used to avoid repeated block of code. It also helps the programmer in readability and maintainability. As we also know that calling a function takes time when it jumps from the position in the memory where it is called. At this point it takes a lot of CPU time which is to some extent non-negligible. There is no doubt about it that function code is easier to read but a lot of overhead is associated when it creates a stack frame on the run-time stack. C++ has the capability which can easily take us out of this dilemma that is the use of the function called the inline function. The compilation procedure as compared to the normal function is different. In a normal function when a function is called, control transfers to the called function. All the parameters and the local variables are pushed onto the run-time stack. Where a stack frame is created not only for these two items but also space is allocated for the return address with the return value if there is any return value. This overhead is resolved by the inline function where the extra stack pushing and popping is rendered. The concept of inline function is the same as macros where text is substituted for the macro call. Technically, an inline function is folded into the code that calls it. This integrated code will make the resulting executable code bigger but if someone look at the overhead side which is an additional call and function calls, which makes the execution a little bit slower.
Before writing the code for inline function, let us see the general format that how it can be declared syntactically correct.
General Format of Inline Functions:
qualifier result-type fname(parameter-list)
Besides the qualifier, they have been explained previously. The qualifier is the reserved word inline that advises the compiler to integrate a copy of the function code in place of a function call. This simply avoids the function call. Suppose there are five calls, the compiler will insert five copies of the function into the program. This will be considered as localized code. Let us consider a program inline.cpp that demonstrates the use of inline function.
- //program name: inline.cpp
- inline int largest(int x, int y);
- int main()
- int n1,n2;
- cout<<”Enter 2 integer #’s to find the largest\n”;
- cout<<”the largest of 2 #’s is: “<
- int largest(int x, int y)
- return((x > y)?x:y);
Enter 2 integer #’s to find the largest
The largest of 2 #’s is: 50
The function “largest” takes two int parameters and returning the largest of those two values. In the function definition head the repetition of the reserved word inline is avoided which seems just the normal function call. In the function prototype, it is the same as the normal function except that it is prepended by the reserved word inline just before the result-type or the return-type.
Now we know that there are overheads involved in calling a function. So for a short function that is less than five lines, it is advisable to use inline functions. It is as fast as macro. Marcro has some limitations. It cannot access the member data of a class whereas an inline function has the privilege of access.
If a function code is large enough, the compiler cannot be forced to inline a function. You can recommend a function to be inline function, the rest is up to the compiler whether to honor the request or not.
As mentioned, macros are the same as inline function ignoring the existence of classes for the movement. But still macros are not much used in C++. Consider a macro that finds the larger of two numbers as we did in inline function.
#define largest(x,y) (x>y)?x:y)
Look closely, this macro is more prone to errors as compared to the previous function that is inline int largest (int x, int y) due to the extra parentheses.
There are so many problems with macros but on the other hand function encompasses overheads that can slower down the process of execution.