Inline Function in C++ | An Important Ingredient for Programmer

In our previous article, we have studied about Virtual Function in C++. If you’re eager to learn about Inline function in C++, then this tutorial would be best suitable for you. C++ Inline functions play a major role in increasing the efficiency of your program. So let’s not waste time and move further to explore the concept.

Keeping you updated with latest technology trends, Join DataFlair on Telegram

What is Inline Function in C++?

Inline Function is nothing but simply a function for which the C++ compiler produces a copy of it every time the function is called during compilation. It is an important feature that rose out from classes and objects in C++

If any changes are made to an inline function, the section of the code containing the function is again compiled and necessary changes in the copies of the code are made, otherwise, it would continue to work according to the old code.

In order to indicate that a function is inline, we use the keyword “inline” in C++.

Inline Function in C++

Syntax of Inline Function in C++

The general syntax for declaring an inline function in C++ is:

 inline return_type function_name( arguments )
{
// BODY OF THE FUNCTION
}

Inline functions can be declared with or without the help of Classes and Objects.

Advantages of Inline Function in C++

An inline function differs from a normal function in many aspects. One of which is that inline functions help us to avoid function calls overhead.

Moreover, C++ inline functions help us in performing push and pop operations in a stack by saving the overhead during function calls. Not only this, but inline functions also enable us to perform content specific optimization on the function body.

C++ Inline Function Example

We are going to learn C++ inline functions with and without classes example. We can implement inline functions in both ways.

Without Classes

Let us better understand the implementation of inline functions with the help of a C++ program without using Classes. In this program, we will find a minimum of two numbers from a set of 4 numbers each.

#include <iostream> 
using namespace std;

inline int min(int n1, int n2) 
{
return (n1 < n2)? n1 : n2;
}

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

cout << "The minimum number out of 37 and 73 is: "<< min(37,73)<<endl;
cout << "The minimum number out of 54 and 9 is: "<< min(54,9)<<endl;
cout << "The minimum number out of 131 and 14 is: "<< min(131,14)<<endl;
cout << "The minimum number out of 9 and 32 is: "<< min(9,32)<<endl;

return 0;
}

Code-

Example of Inline Function in C++

Output-

Output of Inline Function in C++

With Classes

Let us better understand the implementation of inline functions with the help of a C++ program using classes:

In this program, we will perform the operations associated with a rectangle using with member functions to find its area, perimeter and diagonal.

#include <iostream> 
#include<math.h>
using namespace std;

class Rectangle 
{ 
//private by deafult
float length, breadth, area, perimeter, diagonal;
public: 
void input();
void Perimeter(); 
void Area(); 
void Diagonal(); 

}; 
inline void Rectangle :: input() 
{ 
cout<<"Enter the length of the rectangle: ";
cin>>length;
cout<<"Enter the breadth of the rectangle: ";
cin>>breadth; 
} 

inline void Rectangle :: Area() 
{ 
area = length * breadth; 
cout<<"The area of the rectangle is: "<< area <<endl; 
} 

inline void Rectangle :: Perimeter() 
{ 
perimeter = 2 * (length + breadth);
cout<<"The perimeter of the rectangle is: " << perimeter <<endl; 
} 

inline void Rectangle :: Diagonal() 
{ 
diagonal = sqrt((length*length)+(breadth*breadth)); 
cout<<"The diagonal of the rectangle is: " << diagonal <<endl; 
} 

int main() 
{

cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;

Rectangle r; 
r.input(); 
r.Area(); 
r.Perimeter(); 
r.Diagonal(); 
return 0; 
}

Code-

Inline Function in C++ with Example

Output-

Inline function in C++ with results

Limitations of Inline Functions

After developing an understanding as to how we can implement inline functions in C++, let us move towards understanding the cases in which inline functions would not work:

  • Inline functions do not work if the body of the function contains any sort of looping or iteration.
  • Inline functions do not support the use of switch or goto statements.
  • C++ Inline functions cannot work if the function defined is recursive in nature.
  • No other return type except void is allowed while defining the inline function.
  • The inline function should not contain variables with the keyword static.

It’s time to explore Recursion in C/C++ with example

Summary

Now, we have reached towards the end of this tutorial. Up till here, we are now well acquainted with Inline Functions in C++. We learned each and every concept of Inline Functions in detail. We understood how to implement it with and without classes.

The next step – Friend Function in C++ with Examples

If you still find any difficulties, then contact us in the comment section, we’ll help you in the best possible ways.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.