Inline Function


One disadvantage of using normal function is that it will decrease the execution speed of the program. Because every time a function is called the control of the program is transferred to the function body, after executing all the statements inside function the control of the program is transferred immediately after the statement from which the function is called. Thus it will take lots of extra time when function is small and it needs to be called repeatedly.
This problem can solved using the concept of inline function. In order to make a normal function inline you just have to precede the function definition with inline keyword. The inline function must be defined before it is called in the program. It means at the starting of the program.
When the function is defined as inline the function call is replaced by function definition at the time of function call. So there it will increase the execution speed of the program.

     


The general form of inline function is given below:

inline Return-type Function_ Name (Argument_List)
{
Block of statement
}

Advantage of inline function is that it will increase the speed of program execution. You should declare the function as inline only when the function contains two or three statements.
Disadvantage of inline function is that each time a function is called it will replace by function definition so extra memory space is occupied at the time of executing the program.

Example:
#include <iostream.h>
int sum (int a , int b)
{
return a + b;
}
int main ()
{
int x, y;
cout << "Enter Two Numbers";
cin>>a>>b;
cout<<"Sum="<<sum(x, y);
return 0;
}
Output:
Enter two numbers
1
2
Sum=3

Download Projects


Download Programs