Inline Function in CPP

Inline Functions

C++ provides an additional feature which is known as the inline function. This is a powerful concept which is commonly used with classes. If a function is declared inline, then the compiler makes a copy of the function code at each checkpoint where the function is called at compile time.

Inline functions and Classes

The inline function may also be defined inside a class.In actuality, all of the class’s declared functions are implicitly inlined.All inline function limitations are thus also applicable here.Simply declare the function inside the class and define it outside the class using the inline keyword if you need to explicitly declare an inline function in the class.

Syntax for Inline Function:

For creating Inline Function, use the ‘inline’ keyword.

inline returnType function(parameters)
{
  // code
}

Example 1:

Run
#include<iostream>
using namespace std;
inline int Min(int a, int b) {
return (a < b)? a : b;
}
// Main function
int main() {
cout << "Min (30,15): " << Min(30,15) << endl;
cout << "Min (50,400): " << Min(50,400) << endl;
cout << "Min (500,2000): " << Min(500,2000) << endl;
return 0;
}

Output:

Min (30,15): 15
Min (50,400): 50
Min (500,2000): 500

Advantages of Inline Function:

  • The compiler does not comply with the programmer’s request to make a function with a return statement that is marked as inline and without returning any value an inline function.
  • If a function requests to be inlined but contains switch or go-to statements, the compiler rejects the request.
  • It is not possible to convert a function with static variables into an inline function.

Example 2:

Run
#include<iostream>
using namespace std;
inline int pro(int x,int y)
{
   return x*y;
}
main()
{
   int a=5,b=7,res;
   res=pro(a,b);
   cout<<"product of "<<a<< " and " <<b<<" is "<<res;
}

Output:

product of 5 and 7 is 35

Disadvantages of Inline Function:

  • You must recompile the application after making any changes to the inline function code to verify that it is current.
  • The size of the binary executable program has grown as a result of code expansion.
  • Due to the larger executable, an increase in page faults results in subpar application performance.

Are Macros and Inline functions both the same?

  • No, usage of macros is completely discouraged in C++ and unnecessary .they cannot access the private data of a class.so, inlining is recommended than macros. inline functions are capable of type checking and perform casting if required for arguments whereas macros just replace the code
  • Inlining is under the control of compiler whereas  macros are under the control of preprocessor

Can Virtual functions be Inlined?

  • No, virtual function calls are resolved during the execution i.e it gets clarity about the function to be executed in such case how the function can be inlined  as it doesn’ t to know what code to be replaced  at compile time 
  • Inline functions are complied timed and virtual functions are run timed 

Example 3:

Run
#include<iostream>
using namespace std;
inline void welcome()
{
  cout<<"welcome to cpp";
}
main()
{
  welcome();
}

Output:

welcome to cpp

Are Inline Functions really helpful despite many complexities?

  • Yes, even though failure rate of an inline function is high,  they can be really powerful in case of taking certain measures to enjoy the fruitful result such as, keeping the number of inline functions in a program to the minimum and avoid inlining of large functions.

For more C++ content : Click-here