/    /  CPP – Inline Function

Inline Function

 

The C++ online function is a powerful concept widely used by classes. If a function is online, the compiler places a copy of the code of that function at each point where the function is called at compile time.

Any change to an inline function could require the program to be recompiled because the compiler would need to replace all the code once again otherwise it will continue using the old features.

 

Example Program:

#include <iostream>
using namespace std;
inline int Max(int x, int y)
{
return (x > y)? x : y;
}
// Main function for the program
void main( ) {
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
}

 

Output:

 

Some of the situations in which inline functions may not work are:

✓ For functions returning values, whether there is a loop, switch, or goto.

✓ For functions that do not return values, if an instruction return exists.

✓ If the functions include static variables.

✓ In case the inline functions are recursive.