You are on page 1of 6

Inline function

 Steps needed to perform when a function is called,


◦ Jumping to the function
◦ Saving into registers
◦ Pushing arguments into stacks
◦ Return back to calling function
Needs extra time to perform the specified task.
 Macros are the only solution to save memory space
 But macros are not a function that wont carried out the task
of error checking during compilation
Inline function
Primary Factors:

◦ Code Duplication in program is avoided.


◦ Memory space is saved.
◦ C++ provides a mechanism called inline function .
When a function is declared as inline the compiler
copies the code of the function in calling function i.e
function body is inserted in place of function call
during compilation.
◦ Passing of control between coller and collee function
is avoided.
 The compiler replaces the function call with actual
code.
Syntax : Inline function-header
{
function body;
}
Eg : inline double cube(double a)
{
return( a * a * a);
}
C = cube(2.5);
 D = cube(2.5 + 1.5);

Inline function may not suitable for the following situations


 For functions returning values, if a loop, switch, or goto
exists.
 For functions not returning values, if return statement
exists.
 If functions contain static variables.
 If inline functions are recursive.
#include<iostream.h>
Using namespace std;
Inline float mul(float x, float y)
{
Return (x * y);
}
Inline double div(double p, double q)
{
Return(p/q);
}
int main()
{
Float a = 12.345;
Float b = 9.82;
Cout<<mul(a,b);
Cout<<div(a,b);
Return 0;
}
The benefits of inline functions are as follows :
1. Better than a macro.
2. Function call overheads are eliminated.
3. Program becomes more readable.
4. Program executes more efficiently.

You might also like