Inline Function
Inline Function
Syntax:
<inline> <return-type> <function-name>
{
Statement _1;
Statement _2;
.
.
Statement _n;
#include<iostream>
using namespace std;
int main()
{
cout<<max(10,20);
return 0;
}
main()
{ In inline function compiler replaces the function call with
the corresponding function code rather than calling the
cout<<(10>20?10:20); function definition.
return 0;
1
Inline mechanism increases the execution performance in terms of speed. The overhead of repetitive
functions call and returning values are removed. The inline function is mostly useful when calling
function for only small programs. The speed benefits of inline function diminish/removed as the function
grows in size.
On the other hand, the program using inline functions need more memory space since the inline
functions are copied at every point where the function is invoked.
Note: Inline function simply sends a request not a command to the compiler. The compiler may
ignore this request if the function definition is too long or too complicated and compile the function as a
normal function.
Some of the situations where inline function may not work are:
----------------------------------------------------------------------------------
1. If function containing control structure statements such as if, switch, for loop etc.
2. If functions are recursive
3. If function contain static variable
4. The main() function cannot work as inline.
The inline functions are similar to macro of C programming language. The main limitation with macro
is that they are not functions and errors are not checked at the time of compilation. The function offers
better type testing and do not contain any side effects as present in macro.
#include<iostream>
using namespace std;
#define SQUARE(j) j*j
int main()
{
int p=3,q=3,r,s;
r=SQUARE(++p);
s=square(++q);
cout<<endl<<"r="<<r<<endl;
cout<<"s="<<s;
return 0;
}
Output:
2
Question: Write an Inline function in C++ that calculates and prints the volume of
a sphere. Use an inline function sphereVolume that returns the result of the
following expression: (4.0 / 3.0 * 3.14 * pow (radius, 3)).
Ans:
#include<iostream>
#include<cmath>
using namespace std;
inline double sphereVolume(double radius)
{
double PI=3.14,sVol;
return sVol;
int main()
{
double radius;
cout << "Input the radius of the sphere: ";
cin >> radius;
cout << "\nThe volume of the sphere is " << sphereVolume(radius)
<< endl;
return 0;
}
3
Macros
A macro is a piece of code in a program that is replaced by the value of the
macro.
Macro is defined by #define directive.
Whenever a macro name is encountered by the compiler, it replaces the
name with the definition of the macro.
Macro definitions need not be terminated by a semi-colon(;).
Object-like Macros:
#include<iostream>
using namespace std;
#define LIMIT 5
// Driver Code
int main()
{
// Print the value of macro defined
cout << "The value of LIMIT is " << LIMIT;
return 0;
}
Function-like Macro:
#include <iostream>
using namespace std;
#define PI 3.1416
#define AREA(r) (PI*(r)*(r))
int main() {
4
return 0;
}