C++ Inline Function
one of the objectives of using functions in a program is to save the memory space, which becomes appreciable when a function is likely to be called many times .
however when every time a function is called, it takes a lot of extra time in the executing a series of instructions(statements) for tasks such as jumping to the function, pushing arguments into the stack, saving registers, and returning to the calling function. when a function is small, a substantial percentage of execution time may be spent in such overheads.
one solution of this problem is to use macro definitions, popularly known as macros.
preprocessor macros are popular in c. The major drawback with the macros is that they are not really functions and therefore, The usual error checking does not occur during compilation
C++ programming has a different solution to this problem. To Reduce the cost of calls to small functions, C++ propose a new feature called inline function. it is the compiler replaces the functions call with the
corresponding functions code(something similar to macros expansion),An inline function is a function that is expand in line when it is invoked. the inline functions are defined as follows
function-header
{
function body
…………
}
|
example
cube(double x)
{
(x*x*x);
}
|
the above inline function can be invoked by statements like
c=cube(3.0);
d=cube(2.5+1.5);
|
on the execution of these above statements, the values of c variable and d variable will be 27 and 64 respectively.This is make the inline feature far superior to macros. if the arguments are expressions such as 2.5+1.5 the function passes the value of the expression, 4 in this case.
All we need to do is prefix the keyword inline to the function definition.it is easy to make function inline. all the inline functions in program must be defined before they are called.
we should exercise case making a function inline. The speed benefits of inline functions diminish as the function(methods) grows in size. At some point the overhead of the function call becomes small compared to the execution of the function, and the benefits of inline functions may be lost. usually, the functions are made inline whenever they are small enough to be defined in one or two lines
in such cases, the use of normal functions will be more meaningful
Example
cube(double x)
{
(x*x*x);
}
|
remember that the inline keyword merely sends a request, not a command, to the compiler, The compiler may ignore this request if function definition is too long or too complicated and compiler the function as a normal function.
Some of the conditions where inline expansion may not work here
1. for functions returning values,a switch, if a loop, or a goto exists
2. for functions not return any values, if a return statement exists
3. if functions contain static variable
4. if inline functions are recursive
Note: inline expansion makes a program run fast because the overhead of a function call and return is eliminated. However it makes the program to take up more memory because the statements that define the inline function are reproduced at every point where the function is called. so a track if becomes necessary.
<iostream>
using namespace std;
mul(float x,float y)
{
(x*y);
}
div(double p, double q)
{
(p/q);
}
main()
{
a=34.67;
b=9.67;
std::cout<<mul(a,b)<<"\n";
std::cout<<div(a,b)<<"\n";
0;
}
|