Inline Function in C++

Inline Function

Generally upon encountering function call control from the main function  is transferred to called Function

  • A function declared as inline,  replaces the function call with the function definition  instead of transferring the control to the called function
  • As a  result, inline functions executes within the main function and thereby reduces function overhead
  • It is called an  inline function because it is expanded within  a line i.e compiler replaces the function call with corresponding function code
Inline Function in C++

Creating inline function

Creating an inline function is quite simple just add inline keyword at the function signature

#include<iostream>
using namespace std;

inline int pro(int x,int y)
{
return x*y;
}
main()
{
int a=5,b=7,res;
res=pro(a,b);
cout<<"product of "<<a<< " and " <<b<<" is "<<res;
}

O/P:

product of 5 and 7 is 35

Creating inline function

Creating an inline function is quite simple just add inline keyword at the function signature

#include<iostream>
using namespace std;

inline int pro(int x,int y)
{
return x*y;
}
main()
{
int a=5,b=7,res;
res=pro(a,b);
cout<<"product of "<<a<< " and " <<b<<" is "<<res;
}

O/P:

product of 5 and 7 is 35

Advantages

  • Obviously, application performance is improved as function overhead is reduced
  • Use full in embedded systems as inlined functions have less code

Disadvantages

  • Works only for functions that   have 1 or 2 lines of code
  • Purely Compiler dependent: in some compilers, Inlining is failed when it contains any looping statements, complex expressions recursive functions, static variables,goto-continue statements. in such cases, the compiler may or may not  accept it as an inline function and executes as a normal function
  • A system crash may occur when too  large codes are inlined

Understanding Function Overhead

While working with functions, a considerable amount of time is spent in transferring control from main block and then executing any function that is called and then again returning back the control to main block.

The compiler performs the following tasks upon encountering a function call –

  1. Save the actual arguments
  2. Save Return address
  3. Control is transferred from main to called function
  4. Execute the function and return the result
  5. Transfer the control to main()
  6. Retrieve return address and actual arguments
  7. Continue normal flow

Problem

A function call takes lots of extra time in doing the above series of tasks even for a small function which is known as function overhead(control switching time)

Solution

The solution here is inline functions, which reduce the cost of calls in small functions by code replacement upon function call.

All the above actions are skipped in case of an inline function as no control transfer happens. As a result, the function is executed within the main itself

What is no effect Syndrome in Inline Function

Inlining a function has no effect when the function definition contains only I/O statements like cout and cin .this is because the amount of time for IO operations is more dominated than the time for  incurred function overhead

The below-inlined function is the same  as a normal function

#include<iostream>
using namespace std;

inline void welcome()
{
cout<<"welcome to cpp";
}

main()
{
welcome();
}

O/P

welcome to cpp

Are Macros and Inline functions both the same?

  • No, usage of macros is completely discouraged in C++ and unnecessary .they cannot access the private data of a class.so, inlining is recommended than macros. inline functions are capable of type checking and perform casting if required for arguments whereas macros just replace the code
  • Inlining is under the control of compiler whereas  macros are under the control of preprocessor

Can Virtual functions be Inlined?

  • No, virtual function calls are resolved during the execution i.e it gets clarity about the function to be executed in such case how the function can be inlined  as it doesn’ t to know what code to be replaced  at compile time 
  • Inline functions are complied timed and virtual functions are run timed 

Are Inline Functions really helpful despite many complexities?

Yes, even though failure rate of an inline function is high,  they can be really powerful in case of taking certain measures to enjoy the fruitful result such as, keeping the number of inline functions in a program to the minimum and avoid inlining of large functions. Upon following such measures  purpose of inline function (execution speed and code optimizations)  are definitely achieved in real time