Inline functions in C++ | Inline member functions in C++



Inline functions in C++ | Inline member functions in C++

Inline functions are an enhancement provided by C++ to speed up the programs. The coding of normal functions and inline functions are same except that inline function’s definition starts with the keyword inline.

With inline code, the compiler replaces the function call statement with the function code itself and then compiles the entire code. Thus, with inline functions, the compiler doesn’t have to jump to another location to execute the function and then jump back. Because the code of the called function is already available to the calling program.

Inline functions run a little faster than the normal functions as the there is no jumping of control from one memory location to another. But it may result in wastage of memory space

If inline function is called multiple times, function call will be replaced with the code of the function hence more memory space will be consumed by the instructions. An inline function should be placed above all the functions that call it. The inline functions should be very small. Because large inline functions will result in heavy memory usage.

Inline functions can’t be used for following situations

  • For functions that return values.
  • For functions containing a loop or  switch or goto statement.
  • For functions not returning values  if a return statement exists.
  • For functions which contain static variables.
  • If the function is recursive.

The member functions of a class are inline by default so very small member functions should be defined within the class definition. The member function defined outside the class can be made inline explicitly by putting keyword inline before the function definition.

Advantages

  • It does not require function calling overhead.
  • It also saves overhead of variables push/pop on the stack, while function calling.
  • It also saves overhead of return call from a function.
  • It increases locality of reference by utilizing instruction cache.

Disadvantages

  • It may increase function size.
  • It may result in compilation overhead because if some code inside an inline function is changed, then all calling location will also be compiled.
  • If we use inline functions in a header file, it will make size of header file large and make it unreadable.
  • It is not preferred for embeded system where large binary size is not preferred.

//Program to demonstrate the normal inline function

#include<iostream>
using namespace std;
inline void show()
{
 cout<<”Hello how are you”;
}
int main()
{
  show();
  return 0;
}

Output

Hello how are you

Inline Member functions in C++

By default, member functions of a class are inline but we can also use keyword inline to declare them inline  if they are defined outside the class.

//Program to demonstrate the inline member function of a class

#include<iostream>
using namespace std;
class demo
{
 public:
   void show();
};
inline void demo::show()
{
 cout<<endl<<”Hello”;
}
int main()
{
demo obj;
obj.show();
return 0;
}

Output

x=10



Spread the love
Lesson tags: do while loop in C++, do while statement in c, Program of do while statement in C++
Back to: C++ Programming Notes
Spread the love