Categories: C++

Inline Function in C++

What is the Inline function & why is it used?
  • When the program executes the function call instruction, the CPU saves the memory address of the instruction that follows the function call, copies the function’s arguments to the stack, and transfers control to the specified function.
  • This process can occasionally result in overhead in function calls, mainly if the function is small and its execution time is less than the switching time.
  • The inline functions are used to solve this problem. These functions eliminate overhead while also making the program faster by shortening the execution time.

An inline function is one that is expanded in line when called. When an inline function is called, its entire code is inserted or substituted at the point of inline function call. At compile time, the C++ compiler performs this substitution. If the inline function is small, it may increase efficiency.

Syntax :
inline return-type function-name(parameters)
{
    // function code
}  
Example:
#include <iostream>
 
using namespace std;

inline int Min(int x, int y) {//inline function
   return (x < y)? x : y;
}

// Main function for the program
int main() {
   cout << "Min (20,10): " << Min(20,10) << endl;
   cout << "Min (0,200): " << Min(0,200) << endl;
   cout << "Min (100,1010): " << Min(100,1010) << endl;
   
   return 0;
}
Output:
Min (20,10): 10
Min (0,200): 0
Min (100,1010): 100
Key points to remember:
  • Smaller inline functions are more efficient and produce better results than longer inline functions.
  • Converting larger functions to inline may result in code bloat and a decrease in program performance.
  • Always try to define large functions outside the class, because functions defined inside a class are automatically defined as inline, which has a negative impact on the program.
Limitations of Inline function:
  • Many embedded systems may not benefit from inline functions. Because code size is more important than speed in embedded systems.
  • Inlining too much can also reduce your instruction cache hit rate, reducing the speed of instruction fetch from the cache memory to primary memory.
  • If someone changes the code inside the inline function, all the calling locations must be recompiled because the compiler would need to replace all the code once again to reflect the changes, otherwise, it will continue with the old functionality.
Forward Declaration:

Forward declaration is the pre-declaration of the syntax or signature of an identifier, variable, function, class, or other object prior to its use.

Example:
#include <iostream>
using namespace std;

// Forward declaration
class A;
class B;

class B {
 int x;

public:
 void getdata(int n)
 {
  x = n;
 }
 friend int multi(A, B);
};

class A {
 int y;

public:
 void getdata(int m)
 {
  y = m;
 }
 friend int multi(A, B);
};
int multi(A m, B n)
{
 int result;
 result = m.y * n.x;
 return result;
}

int main()
{
 B b;
 A a;
 a.getdata(5);
 b.getdata(4);
 cout << "The Multilication is : " << multi(a, b);
 return 0;
}
Output:
The Multilication is : 20

Note: also read about Types of Class Member Functions in C++

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago