Inline Functions
Welcome back, and as you might have noticed we will be talking about inline functions, the advantages, disadvantages, and good times to use inline.
First, lets start with inline functions in general and what they are. A very short definition is that an inline function is just a basic function that is prefixed by the keyword inline.
 | |  | | |
// Example of an inline function
inline int abs(int r)
{
return r > 0 ? r : -r;
} |
|  |
|
The difference between an inline and a regular function is that instead of calling the function, the compiler copies the function on the spot. So by looking at the above function, you will see that everywhere that you call the function abs(), the compiler will just take the value you pass to the function and replace that code with:
 | |  | | |
// inline example of compiler action
// if you call it like this:
num = abs(num);
// it will translate it to:
num = num > 0 ? num : -num;
// before it compiles it...
// like wise,
// saying the following:
num = abs(13 * 7 - 3);
// will produce:
num = 88 > 0 ? 88 : -88; // num = 88; positive
|
|  |
|
Advantages / Disadvantages of inline Functions
Lets start with the advantages of an inline funciton.
An inline function is about 10000+ times faster than a normal function. An inline function will help with your program's performance by a great deal.
Now the Disadvantages.
An inline function gets compiled as many times as you call it and it also effects filesize.
Now, these advantages and disadvantages are part of the choice and/or trade off's that you have to make as a programmer. Speed over filesize or readability over lines of code, those are the types of choices that you as a programmer will have to make.
When to use an inline function
Its a good idea to use inline for some of the things that you wouldnt call many times from many different places in the code, but rather if you have something like an abs function and you need to loop through an array of values to get the absoulte value of each element in the array, it will speed up your program by an incredible lot. But for things that will cause dramatic filesize, its best to use a regular function as its only compiled once. If you go into many of the standard libraries you will notice many of the math functions are inline. You will also notice that many of the convertion/formatting functions are inline. That is because those type of functions are usually called within an array and are not called as many times as, say "cout".
 | |  | | |
// inline in a loop
#include <iostream.h>
// prototype of some function that fills an array with sin chart
void fillArray(int theArray[], int arraySize);
int main(void)
{
const int MAX_SIZE = 100;
int myArray[MAX_SIZE];
int myPositiveArray[MAX_SIZE];
fillArray(myArray, MAX_SIZE);
for( int i = 0; i < MAX_SIZE; i++ )
myPositiveArray[i] = abs(myArray[i]); // will make it positive
return 0;
} |
|  |
|
It will compile it into the for loop once only. So, not only is it faster than a regular function, but it also requires less file size. But for a function like cout to be made into an inline, it will cost you more than it will do you good.
One last thing that I should mention is that you cannot have a prototype for an inline function, so you have to declare and define the function before the main. Also, if you use a class and you define any method inside of the class declaration, then that method is by default an inline one. It is made into an inline for obvious reasons, to make that method as fast as possible - duh.
Well, that does it for inline functions. Ill see you in the next section!Please Rate this Code:
Comments for: Inline Functions
Annonymouse User says:
Good code, could do with a clearer explaination on how the reason of inline functions. Rock on. |
 |
 |
Annonymouse User says:
Good code, could do with a clearer explaination on how the reason of inline functions. Rock on. says:
Great page helping the understanding of inline functions, although is there not also a method of in-lining where the code fro a function is included in the prototype?
void PlayerHealth() { health++; } |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The general rule is:
When a function is not too complex, its ok to use inlining... for example, if you have any kind of loop, that is considered too complex, and alot of compilers will cry about it. Also, it will not really help you much to use inlining on relatively complex functions. |
 |
 |
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
The general rule is:
When a function is not too complex, its ok to use inlining... for example, if you have any kind of loop, that is considered too complex, and alot of compilers will cry about it. Also, it will not really help you much to use inlining on relatively complex functions. says:
cout is not a function, but an object |
 |
 |
asd [asd@here.com] Posted: 2 times. says:
cout is not a function, but an object |
 |
 |
|
 |
|
 |