How to implement Inline functions in C++?

Filed Under: C++
C Inline Functions

Hey, folks! In this article, we will be unveiling a very powerful function offered by Inline functions in C++ .


Usage of Inline functions in C++

In our traditional compiling system(execution system), whenever a compiler encounters a function call statement, it usually stores the address of the memory in some specified location, and the execution transfers it controls to the actual function block.

After the execution of the function, the value returned by the function gets stored in some register and eventually, the control is transferred again to the function call statement.

We can sense that this is actually a cumbersome and long process which may consume some amount of time adding to the time complexity of the program. Moreover, at times the time taken to transfer the function call to the actual function block is even more than the actual execution period in some of the small functions.

This is when inline functions come into picture.

Inline functions replace the function calls with the function blocks i.e. they expand the function call with the function code. Thus, it reduces the overhead of function calls by the compiler.


Creating an Inline function

As mentioned above, the inline functions reduce the time needed to call and execute the entire function block by the compiler.

When the compiler encounters an inline function, it replaces the function and substitutes the function call with the function blocks for execution.

Syntax:

inline return_type function(argument list)
{
    // Body
}  

Example:

#include <iostream> 
using namespace std; 
inline int multiply(int item1, int item2) 
{ 
	int res =  item1*item2;
	return res;
} 
int main() 
{ 
  int a,b;
  cout<<"Enter the elements:"<<endl;
  cin>>a;
  cin>>b;
  int opt=multiply(a,b);
  cout<<"Result: "<<opt;
}  

Output:

Enter the elements:
10
20
Result: 200


Inline functions within a Class in C++

The Inline function can be clubbed within a class and still can attain the same functionality. In general all the functions declared inside the class automatically behaves like an inline one i.e. they are implicitly inline.

Syntax:

class class-name 
{ 
public: 
    function declaration 
}; 
  
function definition
{ 
  
}

Example:

#include <iostream> 
using namespace std; 
class Evaluate
{ 
public: 
    int multiply(int x,int y);
};
inline int Evaluate::multiply(int item1, int item2) 
{ 
	int res =  item1*item2;
	return res;
} 
int main() 
{ 
  int a,b;
  cout<<"Enter the elements:"<<endl;
  cin>>a;
  cin>>b;
  Evaluate ev;
  
  int opt=ev.multiply(a,b);
  cout<<"Result: "<<opt;
}  

Output:

Enter the elements:
10
20
Result: 200

Advantages of Inline functions

  • Inline functions reduce the overhead of compile-time function calls.
  • It reduces the time required to transfer the call to the function and to execute the function.
  • It eliminates the time required to transfer the control to the function call after the execution of a program.
  • This function can be used majorly for speeding up programs and is useful when even a few seconds matter.

Limitations

  • Inline functions cannot be used for a comparatively large set of functions.
  • Moreover, inline functions won’t work with recursive functions, loops, etc.
  • The compiler decides if a function should be inlined or not. Adding the “inline” keyword does not guarantee that the function will be inlined

Conclusion

Thus, in this article, we have understood the working of C++ Inline functions along with its pros and cons.


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages