C++ Inline function
An inline function is a function that is elaborated in line when it is invoked. When an inline function is called, all the code of inline function gets inserted at the point of function call. It increases the execution time of the program. This function uses the inline keyword before the function name. Syntax
inline return_type function_name(parameter_optional){
function body
}
C++ inline Example 1
#include <iostream>
using namespace std;
inline int mul(int x,int y)
{
return (x*y);
}
int main()
{
int a=5,b=6;
cout << mul(a,b) <<"\n";
return 0;
}
30
C++ inline Example 2
It is a good experience to write C++ program using class. The functions which are declared inside the class are implicitly inline function. An explicit inline function is declared inside the class and defined at outside the class.#include <iostream>
using namespace std;
class example
{
int a,b,add;
public:
void get();
void sum();
};
inline void example :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
inline void example :: sum()
{
add = a+b;
cout << "Addition of two numbers: " << a+b << "\n";
}
int main()
{
cout << "Program using inline function\n";
example e;
e.get();
e.sum();
return 0;
}
Program using inline function Enter first value :5 Enter second value :10 Addition of two numbers: 15