EXP-2 Inline functions
EXP-2 Inline functions
In C++, we can declare a function as inline. This copies the function to the location of the
function call in compile-time and may make the program execution faster.
C++ provides inline functions to reduce the function call overhead.
An inline function is a function that is expanded in line when it is called.
When the inline function is called whole code of the inline function gets inserted or substituted
at the point of the inline function call.
#include <iostream>
using namespace std;
int main() {
// first function call
displayNum(5);
return 0;
}
Output
5
8
666
#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
Output
The cube of 3 is: 27
#include <iostream>
class operation {
int a, b, add, sub, mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation ::get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
Output:
Enter first value: 45
Enter second value: 15
Addition of two numbers: 60
Difference of two numbers: 30
Product of two numbers: 675
Division of two numbers: 3