Normally, a function call transfers the control from the calling program to the called function. This process of jumping to the function, saving registers, passing a value to the argument, and returning the value to the calling function takes extra time and memory.
An inline function is a function that is expanded inline when it is invoked, thus saving time. The compiler replaces the function call with the corresponding function code, which reduces the overhead of function calls.
When an inline function is called, the compiler replaces all the calling statements with the function definition at run-time. Every time an inline function is called, the compiler generates a copy of the function’s code, in place, to avoid the function call.
inline return_type function_name(parameters)
{
// function code
}
#include <iostream>using namespace std;inline int max_num(int x, int y) {return (x > y)? x : y;}// Main function for the programint main() {cout << "Greater of ( 40, 50 ) is: " << max_num(40,50) << endl;cout << "Greater of ( 90, 50 ) is: " << max_num(90,50) << endl;cout << "Greater of ( -5, 34 ) is: " << max_num(-5,34) << endl;return 0;}
Free Resources