Home » A Brief Discussion on C++ Inline Functions

A Brief Discussion on C++ Inline Functions

Of many interesting concepts of C++, the inline functions are quite useful, especially when it is about the performance of a program. Before a function is made inline, it is important to have a thorough knowledge of the inline functions in C++. So, let us discuss it today.

Inline functions in C++

It is a feature of optimization that lessens the time of execution of a program. In a similar way, our macros are there in C. This particular concept of inline functions is used to handle Classes in C++. Anytime one such function is encountered, it is replaced with a copy of the code of that function by the compiler. In other words, one such function is increased inline. Thus, it is called an inline function.

Inline functions are mostly used when the definitions of functions are small and functions are called multiple times in a program. By using the inline functions in C++, one can save the time to transfer the program control from the calling function to the called function. Nevertheless, the inline function is a request or can even be called a suggestion to the compiler and not at all a mandatory command. Accepting or declining the suggestion is totally up to the compiler.

A compiler can decline the request of inlining function under circumstances such as:

  • As a function with a return statement does not return the expected or any value and is marked as inline, the compiler tends to stop responding to the request of the programmer for making it an inline function
  • As a programmer put efforts to inline a function that contains a loop, the compiler declines the suggestion for inline
  • The compiler also declines the suggestion of the programmer when a function is recursive
  • It can be rejected by the compiler even when a function contains static variables
  • Last but not the least, the suggestion or request can be declined even when it is containing a switch or the go-to statements.

Advantages of inline functions

  • There is no occurrence of functions call overhead
  • Inline functions also save the overhead of push or  pop variables on the stack as the function is called
  • The overhead of a return call from the function is also saved
  • As a function is inlined, the compiler can be allowed to perform context-specific optimization on the function. Such optimizations are usually not possible for the general function calls. By considering the flow of the calling context as well as the context called, the other optimizations can be attained
  • This is useful for embedded systems since inline can yield lesser code as compared to the function call preamble along with the return.

Disadvantages of inline functions

  • Since the code is expanded, the binary executable program size is increased
  • If you make any change in the inline function code, you would be required to recompile the program and make sure that it has been updated
  • A boost in the page fault may lead to bad performance of the program because of the increased executable size
  • The call, as well as return overhead time of the function, is insignificant as compared to the entire execution time, for the systems with huge executable code. However, inlining of the functions for such a program may decrease the speed of the system. Therefore, it may not be a great option.

Inline function and classes

It is very much possible to describe the inline functions inside the class. As a matter of fact, each of the functions described inside the class is indirectly inline. Therefore, the restrictions of inline functions too are applied here. If you find the need to directly declare the inline function in the class, you can declare the function inside the class and describe it outside the class by making use of keywords.

Example:

class S

{

public:

    inline int square(int s) // redundant use of inline

    {

        // this function is automatically inline

        // function body

    }

};

The program mentioned below describes the concept:

#include <iostream>

using namespace std;

class operation

{

    int a,b,add,sub,mul;

    float div;

public:

    void get();

    void sum();

    void difference();

    void product();

    void division();

};

inline void operation :: get()

{

    cout << “Enter first value:”;

    cin >> a;

    cout << “Enter second value:”;

    cin >> b;

}

inline void operation :: sum()

{

    add = a+b;

    cout << “Addition of two numbers: ” << a+b << “\n”;

}

inline void operation :: difference()

{

    sub = a-b;

    cout << “Difference of two numbers: ” << a-b << “\n”;

}

inline void operation :: product()

{

    mul = a*b;

    cout << “Product of two numbers: ” << a*b << “\n”;

}

inline void operation ::division()

{

    div=a/b;

    cout<<“Division of two numbers: “<<a/b<<“\n” ;

}

int main()

{

    cout << “Program using inline function\n”;

    operation s;

    s.get();

    s.sum();

    s.difference();

    s.product();

    s.division();

    return 0;

}

Output:

Enter first value: 45

Enter second value: 15

Addition of two numbers: 60

Difference of two numbers: 30

Product of two numbers: 675

Division of two numbers: 3 

While we discuss C++ inline functions, we need to understand that inline functions are a significant feature of C++. Using it correctly can improve performance. Also, keeping inline functions lesser is better for improved performance of the program.

If you want to gather more knowledge on C++ inline functions, you can visit Great Learning and read some more interesting blogs.Hope this article helps you a lot for understanding this topic. If you’re interested in free online courses with certificates, So enroll today on Great Learning Programme.

Back to top