coderz.py

Keep Coding Keep Cheering!

Inline Function in C++

Accessing Elements
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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement