In this article inline function we give the information about if a function is inline then the compiler keeps the copy of the code of the function in place of the function call and this can speed up the execution of the program.
Function:
A function is used to reduced code redundancy as well as to save the memory space, when a function is invoked a bunch of task is performed namely matching arguments, matching returns and passing the control from calling to definition and vice-versa.
For example:
int area(int l, int b) // function definition
{
return (l*b);
}
area(10,7); // function call
Inline function:
When function definition consists of hardly one or two segments, this bunch of task appears to be time consuming. Hence in order to save this time. C++ has the concept to inline function.
When a function is declared inline the function body is replicated at function calling place.
Note: Function contains only one or two statements
Inline function in C++:-
- In C++, inline function is an important concept which is mostly used with class.
- If a function is inline then the compiler keeps the copy of the code of the function in place of the function call and this can speed up the execution of the program.
- In C++, the overhead of function calls is reduced by using inline functions.
- The inline keyword is used to create the this function.
Syntax:
The inline keyword is used to create inline functions. Its syntax is given below.
inline returnType functionName(parameters)
{
// code
}
Advantages of function in C++:
- It reduces the overhead of function call, which makes the execution of the program faster.
- It also saves the overhead of push/pop variables in the stack when the function is called.
- It also saves the overhead of return call from a function.
- It is useful for embedded systems.
Disadvantages of function in C++:
- The number of variables is increased by the inline function. Due to which the utilization of registers becomes more.
- If you use too many inline functions, then the same code gets duplication. It is very bad to have the same code over and over again.
- By using more inline functions, the speed of fetching the instruction also decreases.
Note:
The point to note here is that inlining is a request to the compiler, it is not a command. The compiler can also ignore the request for inlining.
It ignores the request for this in the following circumstances:-
- If there is a loop in the function (eg:- for, while, do-while etc.).
- If the function contains static variables.
- If the function is recursive.
- If there is a switch command or goto statement in the function.
Program:-
#include<iostream>
using namespace std;
inline int add(int x,int y)
{
return x+y;
}
int main()
{
int a,b;
cout<<”\n Enter first number:”;
cin>>a
cout<<”\n Enter second number:”;
cin>>b;
cout<<”\n Sum of a & b is:”<<add(a,b);
return 0;
}
OUTPUT:
Enter first number:
7
Enter second number:
8
Sum of a & b: 15
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining