inline functions C++

Out of several exciting concepts of C++, the inline functions can prove to be pretty useful when it comes to the performance of a program. Before making a function inline, it is quite essential to have the know-how and know-what of the inline functions. Without any further ado, let’s jump straight into our topic!

Inline Function in C++

An inline function in C++ is an optimisation feature that reduces the execution time of a program, just as we have macros in C. This concept of inline functions is used while handling Classes in C++. It is used otherwise as well, though. Whenever such a function is encountered, the compiler replaces it with a copy of that function’s code (function definition). In other words, one may say that such a function is expanded in-line, hence named as an inline function.

Inline functions are commonly used when the function definitions are small, and the functions are called several times in a program. Using inline functions saves time to transfer the control of the program from the calling function to the definition of the called function. 

However, inlining a function is just a request or suggestion to the compiler, not any mandatory command. It depends on the compiler whether to accept or decline this suggestion of inlining a particular function. The compiler is most likely to not consider the inlining of a function under certain circumstances that are mentioned here as follows-

  • When a function with a return statement is not returning any value and marked as inline, the compiler does not respond to the programmer’s request of making it an inline function.
  • When a programmer tries to inline a function containing a loop (for, while, or do-while), the compiler refuses the inline suggestion.
  • When a function is recursive, it cannot be inlined.
  • A function containing static variables cannot be made an inline function. 
  • The compiler declines the request of inlining a function if it contains any switch or go-to statements.

The syntax for defining an inline function has been shown below:

inline return-type function-name(parameters){    // function code}  

Example of an Inline Function

In order to make a function inline in C++, the keyword “inline” is used. Here is an example of C++ code explaining the implementation of an inline function:

#include<iostream>
using namespace std;
inline int add(int a, int b) //Inline function definition
{
return a+b;
}

int main()
{
cout<<"The addition of 562 and 451 gives the value:"<<add(562,451)<<"\n"; //Inline function call
return 0;
} 
//Output: The addition of 562 and 451 gives the value:1013


Here, the inline function add(int a, int b) has been defined before the main() function and later been called inside the main () function. When the add function is called, the code of the add function definition is placed there, i.e. inlined at the time of compilation. Henceforth, it saves from the overhead of function calls and returns. The working of the inline function add(a,b) has been illustrated below: 

#include <iostream> 
using namespace std; 
inline int add(int a, int b) 
{     
return a+b:

int main() 
{     
cout << “The addition of 562 and 451 gives the value: “<< add(562,451) << “\n”;     
return 0; 




COMPILATION
##include <iostream> 
using namespace std; 
inline int add(int a, int b) 

    return a+b; 

int main() 

    cout << “The addition of 562 and 451 gives the value: ” << 562+451 << “\n”; 
    return 0; 

If one has to declare the inline function in a Class in C++ explicitly, then declaring the function inside the class and defining it outside the class using the inline keyword is the only requirement. However, an inline function can be defined inside a class as well. Following is an example of C++ code to help you get a better view of implementing an inline function while dealing with classes.

inline int A::add(int a, int b)//Inline function definition
{
return (a>b)?a:b;
} 


class A
{
 public:
    inline int max(int a, int b)//Inline function declaration with definition
    {
       return (a>b)?a:b;
    };
}

class A
{
 public:
    int max(int a, int b);//Inline function declaration
}

At the time of compilation, this inline function max(a,b) in the above example works in the same way as the inline function add(a,b) in the previous example.

Advantages of using Inline Functions

The Inline function is quite a useful feature provided by C++ due to its several advantages that are listed below.

  • No function call overhead occurs; hence enhanced program speed is obtained.
  • It helps in saving the overhead of return call from a function.
  • Helpful while calling a function in saving the overhead of variables push/pop on the stack.
  • The utilisation of the instruction cache increases the locality of reference.
  • Inlining a function often yields less code than the function call preamble and return, thus useful for the small embedded systems.

Disadvantages of Inline Functions

Any reliable feature you name in a programming language comes with its own set of cons, and so is with the inline functions of C++.  Let’s see the disadvantages of inline functions now!

  • Due to code expansion, the size of the binary executable program is increased.
  • Any change in the inline function code would need you to recompile the program to ensure it is updated. 
  • An increase in the page fault leads to poor program performance due to its increased executable size.
  • For the systems with a large executable code, the call and return overhead time of its functions is negligible compared to their whole execution time. But, inlining the functions of such a program can bring down the speed of the system. So, it might not turn out to be a good option always, like in the case of large embedded systems which prefer high speed over sizeable executable size.

Points to be kept in mind

As an overview of the whole topic of inline functions in C++, here are a few essential points necessary to be remembered about the inline functions while using C++ language.

  • Inline functions are only suggestions, not compulsions. It is possible that the compiler does not inline the functions marked as inline by the programmer. Simultaneously, the compiler can decide to inline a function even if it is not marked as inline. 
  • Functions that are declared and defined inside a class need not be defined as inline functions explicitly since they are already treated as inline functions by default.
  • Unlike pre-processor macros, Inline is easy to debug and does not corrupt the namespaces, code as it behaves as a compiler controlled copy & paste action and not implemented forcibly.
  • Except for the conditions where the object’s type is known to the compiler, i.e. when the object has been declared and constructed inside the same function body, virtual methods cannot be inlined.
  • The presence of a method or function used as a template in the header section does not make it automatically inlined. 

This brings us to the end of the blog on Inline Functions in C++. Hope this helps you to up-skill your C++ skills. To learn more about programming and other related concepts, check out the courses on Great Learning Academy

Also, if you are preparing for Interviews, check out these Interview Questions for C++ to ace it like a pro.

0

LEAVE A REPLY

Please enter your comment!
Please enter your name here

1 × five =