Easy Tutorial
❮ C Function Asctime Cpp Conditional Operator ❯

C++ Inline Functions

C++ Classes & Objects

C++ Inline Functions are commonly used with classes. If a function is inline, the compiler places a copy of the function's code at each point where the function is called at compile time.

Any modifications to an inline function require all clients of the function to be recompiled because the compiler needs to replace all the code once again; otherwise, it will continue using the old function.

To define a function as an inline function, the keyword inline must be placed before the function name, and the function must be defined before it is called. If the defined function is more than one line, the compiler may ignore the inline qualifier.

Functions defined inside a class definition are inline functions, even without the use of the inline specifier.

Below is an example that uses an inline function to return the maximum of two numbers:

#include <iostream>

using namespace std;

inline int Max(int x, int y)
{
   return (x > y) ? x : y;
}

// Main function of the program
int 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;
   return 0;
}

When the above code is compiled and executed, it produces the following result:

Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010

C++ Classes & Objects

❮ C Function Asctime Cpp Conditional Operator ❯