Skip to main content

Inline Functions and their Uses

It’s a good practice to divide the program into several functions such that parts of the program don’t get repeated a lot and to make the code easily understandable.

We all know that calling and returning from a function generates some overhead. The overhead is sometimes to such an extent that it makes significant effect on the overall speed of certain complex and function-oriented programs. In most cases, we have only a few functions that have extensive use and make significant impact on the performance of the whole program.

Not using functions is not an option, using function-like macros is an option, but there is a better solution, to use Inline Functions.

Yes, like it sounds, inline functions are expanded at the place of calling rather than being “really called” thus reducing the overhead. It means wherever we call an inline function, compiler will expand the code there and no actual calling will be done.

Member functions of classes are generally made inline as in many cases these functions are short but any other function can be inline too. Functions are made inline by preceding its definition with the keyword “inline” as shown in the example below:

// precede the definition
// of function with "inline"
// it's optional to do so for
// the prototype
inline ret-type func-name(arg-list...)
{
...
...
...
}


Member functions (class) can be made inline as below:

class myclass
{
private:
...
...

public:
ret-type func-name(arg-list...);
...
...
};

inline ret-type myclass::func-name(arg-list...)
{
...
...
}


Or simply as

class myclass
{
private:
...
...

public:
inline ret-type myclass::func-name(arg-list...)
{
...
...
}
...
...
};


Short member functions are usually made inline as above.

Please mote one thing though, inline is a request to the compiler and not a command which means it depends on the compiler and the conditions whether a particular function will be really made “inline” or not.

As inlining increases duplication of parts of code, performance will be gained at the expense of program size, which is pretty obvious. As I’ve said not all functions make major impact on the overall performance of the program. So we should carefully select which functions to inline and which not to, because if we inline many functions we can’t be sure of whether it’d do any better to the performance, but it’s for sure that program size will increase unnecessarily.

Comments

Popular posts from this blog

FOR MY BLOG READER'S

It is not for me but for my readers. 'One' resolution fROM taday: I want to look like Aamir khan (without 8 packs) Bike or Car (and why): Car, I’ve three reasons for it 1. It’s safer than bike 2. I’ve always dreamt of going on a long drive with my soul-mate in a car. 3. While driving a car you can look into the eyes of.......... obviously you can guess?? J A chance to fly or a chance to be invisible (and why): Invisible, I suffer from Acrophobia (an extreme or irrational fear of heights). I would love to love: My parents, they deserve my love more than anyone in this world. Most recent Dream I had: I’m a kid again. Teacher in the school asks to describe a word and I was struggling L If I were to marry today, I would: If the girl is pretty, I would be delighted. If the girl is not so ..., I wouldn’t marry and convince everybody that I’m underage (I’m actually 20 years old). My Dream Girl is/would be: SHEEEE Best F...

100+ Sweet Things to Say to Your Girlfriend

Sweet Things to Say to Your Girlfriend : You know what is the basic reason of splitting up of many couples? It does not happen because they do not love each other enough now, but it happens because they forget to show their love to each other. In a relationship, mostly girls demand some attention and affection from their partners. And all you guys can give it to them with the help of sweet things to say to your girlfriend. Don’t let your ego ever comes between you two. Love has more power than any other thing in the world. Use this power to overcome all the problems in your relationship. These sweet things to say to your girlfriend are nothing when you are not feeling them. These are not just the lines but the feelings you hold in your heart for her but could not display all this while. 100+ Sweet Things to Say to Your Girlfriend To help you more on this, I have compiled a list comprising of more than 100 sweet things to say to your girlfriend. These are simpl...

Geometric Print Sarees to Be the Best Choice for Young Women of India

Today, when great number of women population have move towards the western outfits, there are yet thousands and thousands women wear sarees in the country. This is the reason why saree does not be outdated at all. This is one of the most demanded traditional outfits created in amazing designs and shades. Today, wide range of  machine work sarees  is available in the market If you are a young woman and like to dress yourself in the most fashionable way then you must go with the range of online geometric sarees available at many shopping destinations. Besides, if you are recently married women or working women with saree as the formal then geometric can be the best to go with. These sarees are designed in such way that they maintain simplicity along with the stylishness, have a look on some samples of  geometric print sarees  are giver here- Are not these sarees striking and heart-stealing? These are especially designed for them women of 21 st ...