Inline In C++
One of
the major objectives of using functions in a program is to save memory space,
which becomes appreciable when a function is likely to be called many times.
Fig: Inline In c++ |
Advantages of Inline function
- Function call overhead doesn’t occur.
- It saves the overhead of a return call from a function.
- It saves the overhead of push/pop variables on the stack when the function is called.
Limitations of Inline functions
- Large Inline functions cause Cache misses and affect efficiency negatively.
- Compilation overhead of copying the function body everywhere in the code at the time of compilation which is negligible in the case of small programs, but it may make a big difference in large codebases.
- If we require address of the function in a program, the compiler cannot perform inlining on such functions.
Syntax:
inline returntype functionname (argument
list)
{
function of body;
}
Program:
#include<iostream.h>
#include<conio.h>
inline intmulti(int a, int b)
{
return(a*b);
}
void main()
{
int a,b,c;
cout<< “input two number”;
cin>>a>>b;
c=multi(a,b);
cout<<c;
getch();
}
Output:
Input two number:
45
35
1575
No comments:
Post a Comment