Close
Close

Inline Functions in C++


Though inline functions are not that necessary, but we use them to make our program faster.

Normally, when we call a function, the function first gets called and then its body gets executed. It takes comparatively more time to call a function than to write the code in the program in place of that function call.

For example, take the following two codes.

#include <iostream>

using namespace std;

int main()
{
	int a = 2, b = 3;
	cout << (a + b) << endl;
	return 0;
}
#include <iostream>

using namespace std;

int sum(int x, int y)
{
	return x+y;
}

int main()
{
	int a = 2, b = 3;
	cout << sum(a, b) << endl;
	return 0;
}

We are doing the same thing in both the above programs i.e., calculating the sum of two variables 'a' and 'b'. In the second program, we first called the function 'sum()' which then calculated the sum as x + y and returned it. On the other hand in the first program, we directly printed the expression for sum a + b in the main program. So in the first case, we saved the time of calling the function.

So, if we had to choose one of the above methods to print the sum of two numbers, we would choose the first case which would make our program run faster.

Now, suppose we have to print the sum of many such sets of numbers. Then writing the expression (a+b) would be hectic. Things will become worse if the expression is more complex.

This is when the need of inline function arises.

Inline function works same as the first case where we do not need to call any function. In a program, wherever a function declared as inline is called, the compiler will replace the function call with the code in the function. This saves the time that is taken when calling a function ( as in the second case ) as well as it prevents us from writing the same code again and again ( as in the first case ).

To make a function inline, we need to write the keyword inline before the function name.

Look at the following code.

#include <iostream>

using namespace std;

inline int sum(int x, int y)
{
	return x+y;
}

int main()
{
	int a = 2, b = 3;
	cout << sum(a, b) << endl;
	return 0;
}

Here, we declared the function sum() as inline. So, when this function is called in the main class, the compiler replaces this function call sum(a, b) with the code in the body of the function 'sum()'. So, the above code becomes as shown below:

#include <iostream>

using namespace std;

int main()
{
	int a = 2, b = 3;
	cout << (a + b) << endl;
	return 0;
}
An inline function must be defined at the time of its declaration.

Now let's see an example of inline function.

#include <iostream>

using namespace std;

inline int exp(int x, int y, int z)
{
	return (x + y) * z;
}

int main()
{
	cout << exp(4,5,7) << endl;
	cout << exp(4,5,6) << endl;
	cout << exp(4,7,5) << endl;
	cout << exp(7,4,6) << endl;
	return 0;
}
Output
63
54
55
66
To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.

Don't worry about failure. You only have to do right once.
-Drew Houston


Ask Yours
Post Yours
Doubt? Ask question