Skip to content
Related Articles

Related Articles

Improve Article

Difference Between Inline and Normal Function in C++

  • Difficulty Level : Basic
  • Last Updated : 27 Jul, 2021

Inline Function is a function that is expanded inline by the compiler when it is invoked. During function call, a lot of overhead tasks are performed like saving registers, pushing arguments to the stack, and returning to the calling function. These overheads are time-consuming and inefficient for small-size functions. In C++, the inline function is used to solve these overhead costs. It is expanded in line by the compiler when it is invoked, thus overhead cost is avoided. A keyword known as ‘inlineis used before the function declaration. 

Syntax:

inline return_type function_name(parameters) 
{
// Insert your Function code here
}

Example:

C++




// C++ program to demonstrate inline function
 
// Importing input output stream files
#include <iostream>
 
using namespace std;
 
// Method 1
// Inline function
// To multiply two integer numbers
inline int multiplication(int x, int y)
{
 
    // Returning the producus of two numbers
    return x * y;
}
 
// Method 2
// Main driver method/function
// Execution begins here
int main()
{
    // Print and display the multiplication resultant number
    // with usage of above created multiplication() inline()
    cout << "Multiplication ( 20 , 30 ):"
         << multiplication(20, 30) << endl;
 
    return 0;
}
Output
Multiplication ( 20 , 30 ):600

Now dwelling onto the next function, s the name suggests Normal Function is basically simply a common function in C++. It promotes code reuse and makes the program modular. During function calls, a lot of overhead tasks are performed like saving registers, pushing arguments to the stack, and returning to the calling function. 



Syntax: 

return_type_name function_name( parameters) 
{ 
 // Insert your Function code here 
}

Example: 

C++




// Importing input output streams
#include <iostream>
 
using namespace std;
 
// Method/Function
// To get square of integer number been passed
int square(int s)
{
    // Returning the square of number
    return s * s;
}
 
// Method
int main()
{
    // Calling the square() method/function
    // over randoM value N
     
    // Say N = 5
    // Display message only
    cout << "Enter number to compute its square : 5 " << endl;
 
    // Calling the above square() in main()
    // and print/display on the console
    cout << "Square is : " << square(5) << endl;
   
  return 0;
}
Output
Enter number to compute its square : 5 
Square is : 25

Difference between Inline function and Normal function in C++

S.No.              

Inline Function 

Normal Function  

1.It is expanded inline when it is invoked. It is a function that provides modularity to the program.  
2.It is generally used to increase the execution time of the program.It is generally used to improve the reusability of code and make it more maintainable.  
3.It is basically a function that is used when functions are small and called very often.  It is basically a group of statements that performs a particular task.  It is used when functions are big.
4.It requires ‘inline keyword in its declaration.  It does not require any keyword in its declaration.  
5.It generally executes much faster as compared to normal functions.  It generally executes slower than inline function for small size function.  
6.In this, the body of functions is copied to every context where it is used that in turn reduces the time of searching the body in the storage device or hard disk.In this, the body of the function is stored in the storage device and when that particular function is called every time, then CPU has to load the body from hard disk to RAM for execution.  
7.The compiler always places a copy of the code of that function at each point where the function is called at compile time. It does not provide such a type of functionality.  
8.It generally includes only 2–3-line codes.  When the number of line codes is real massive I.e., normal functions contain much code as per need.  
9.It is a little harder to understand and test as compared to normal function.  It is much easier to understand and test as compared to the inline function. 
10.Functions that are present inside a class are implicitly inline.  Functions that are present outside class are considered normal functions.  
11.Too many inline functions affect file size after compilation as it duplicates the same code.  Too many normal functions do not affect file size after compilation. 

 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :