#Previous question #Index of C++ Questions #Next question

The inline functions are a C++ enhancement designed to speed up programs. The coding of normal functions and inline functions is similar except that inline functions definitions start with the keyword inline. The distinction between normal functions and inline functions is the different compilation process.

Working: After writing any program, it is first compiled to get an executable code, which consists of a set of machine language instructions. When this executable code is executed, the operating system loads these instructions into the computerís memory, so that each instruction has a specific memory location. Thus, each instruction has a particular memory address.

After loading the executable program in the computer memory, these instructions are executed step by step. When the function call instruction is encountered, the program stores the memory address of the instructions immediately following the function call statement, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function codes, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function.

The C++ inline function provides an alternative. With inline code, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program.

Example:

 
inline void max(int a, int b)
{
  cout << (a > b ? a : b);
}
int main()
{
  int x, y;
  cin >> x >> y;
  max(x, y);
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

Most popular topics (must visit)

Useful links for students