What is an inline function

in a word, inline function means a function that expands in the same line where it is called

until now, okay,

in short, we don’t need a separate block for that function at the time of  memory allocation

No hassle right?

to sum up, the inline function will get executed as a part of a main() function only

so far, clear as mud?

to clarify, in C we don’t have keyword inline but, it is added in C99 as a keyword

Note: in reality, there is no guarantee that the code will be inserted at the place where it is called

But, why?

 because, the inline keyword is a request to the compiler, not a direct command. at times, the compiler may ignore our request

But, why do we use it?

  • To reduce the overheads that occur because a small function is getting called several times in a program
  • To save execution time and space(the program runs faster and takes less space)

When we should use it?

Only when the function code is small, if the function is large then, in that case, you should prefer the normal function

Syntax:

inline function name
{
Function body
}

A simple inline keyword example

#include <iostream>
using namespace std;
inline int sub(int x,int y) //inline function
{
    return(x-y);
}
int main()
{
    int a=20,b=15;
    cout<<sub(a,b);// inline function call
    return 0;
}

Output

5

in this situation, pretty effective right?

Here’s another one

Functions that are defined inside the class are by default inline but when a function is defined outside the class, then, in that case, it will be a non-inline function

to clarify, let’s deconstruct an example

#include <iostream>
using namespace std;
class test
{
public:
    void fun1()
    {
        cout<<"inline"<<endl;
    }
    void fun2();//Function prototype declaration
};
void test::fun2()//Function definition outside the class
{
    cout<<"non-inline"<<endl;
}
int main()
{
    test t;
    t.fun1();
    t.fun2();
    return 0;
}

Output

inline
non-inline

to put it differently, is it possible, to make function declared outside the class as inline

in short, absolutely yes!

So, to make the function that is declared outside the class an inline function

 in this situation, we need to write the keyword inline in the function prototype declaration

until now, okay,

to demonstrate, let’s deconstruction an example

#include <iostream>
using namespace std;
class test
{
public:
    void fun1()
    {
        cout<<"inline"<<endl;
    }
   inline void fun2();//Function prototype declaration
};
void test::fun2()//Function definition outside the class
{
    cout<<"non-inline becomes inline"<<endl;
}
int main()
{
    test t;
    t.fun1();
    t.fun2();
    return 0;
}

Output

inline
non-inline becomes inline

Resource

  • Let us C++ by Yashavant Kanetkar
  • https://www.udemy.com/course/cpp-deep-dive/

Mohammed Anees

Hey there, welcome to aneescraftsmanship I am Mohammed Anees an independent developer/blogger. I like to share and discuss the craft with others plus the things which I have learned because I believe that through discussion and sharing a new world opens up

Leave a Reply

Your email address will not be published.