inline fuctions
inline fuctions
Inline functions:
Normal function usually has an overhead i.e (the argument passing & the value returning
activities with a function call) during the execution time of a program. We use inline functions to
avoid these overheads.
Definition:
A function whose body/code is inserted at the place of its function call. They are short
functions.
Inline function is a function that replaces the function call by body of function. An inline function is a
function that is expanded in-line when it is invoked.
Syntax:
inline return-type-specifier function_name (arguments)
{
statements;
return expression;
}
Example 1: Example 2:
inline int cube(int a) inline int max(int a,int b)
{ {
return (a*a*a); return (a>b? a : b);
} }
NOTE: The inline function may not work some time for one of the following reasons (limitations)
Write a C++ Program to find the cube of a number using inline functions
#include <iostream.h>
#include <conio.h>
class line
{
public: inline int cube (int s)
{
return(s*s*s);
}
};
void main()
{
line I;
int x,y;
clrscr();
cout<<“Enter a number”;
cin>>x;
y=I.cube(x);
cout<<“the cube of“<<x<<“=“<<y;
getch();
}
~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~