inline Functions

The use of the keyword inline placed before a function, instructs the compiler to replace all functions calls to the function with a copy of the function code.

 

 

This greatly increases speed since the compiler doesn't need to store the return address from the function call, thus saving time.

 

However, the down side to this is that this can greatly increase the code size, since each function call is now expanded with a copy of the whole function code.

 

Syntax:

inline functionName() {

...code body

}

 

#include <iostream>
using namespace std;

inline float cube(int myVar){
	return myVar * myVar * myVar ;
}

int main() {

	float box1 = 3;

	cout << "The cube of box 1 is: " << cube(box1) << endl ;

	//the compiled equivalent code would be:
	cout << "The cube of box 1 is: " << box1 * box1 * box1 << endl ;

	return 0 ;
}

Compile & Run:

The cube of box 1 is: 27
The cube of box 1 is: 27

Leave a Reply