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

Free C++ course with real-time projects Start Now!!

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.

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.

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

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.

You give me 15 seconds I promise you best tutorials
Please share your happy experience on Google

follow dataflair on YouTube

1 Response

  1. Peter says:

    So functions can be marked “inline” in C++. Some functions (e.g. member functions that are defined inside the class definition) are implicitly inline. This basically means the multiple definitions of the function is allowed and the function has to be defined in all translation units where it’s used.

    There is also an optimization called “function inlining” that compilers can do. It avoids function call overhead, allows the compiler to better optimize for the specific context, etc. It might be easier to do this optimization with inline C++ functions compared to normal C++ functions (not really an issue if you enable “link-time optimizations”) but it’s really up to the compiler. The compiler can choose to do this optimization to non-inline C++ functions and it could choose to not do it with inline C++ functions.

    The limitations you mention are not correct. Inline functions can use loops, switch and goto. They can be recursive (although that might prevent the “function inlining” optimization). Inline functions can return any type that non-inline functions can. Inline functions have no problem with the static keyword.

Leave a Reply

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