Inline Functions

When you define a function in c++, normally the compiler creates one set of instruction in memory. When you call the function, it follow instruction and execute the program. If you call the function 5 times, your program jumps to the same set of instructions each time. This means there is only one copy of the function, not 5. While this sequence of events may save memory space, it takes some extra time.

To save execution time in short functions, you may want to put the code in the function body directly inline with the code in the calling program. If a function is declared with the keyword inline, the compiler does not create a real function: it copies the code from the inline function directly into the calling function

Inline functions can be define using inline keyword.
Eg.

 
inline void funct1()
{
	// statements 
}

Ex

#include<iostream>
using namespace std;

inline double convertweight(double p)
{
    return (p *0.453592);
}
int main()
{
    double kg ,p ;

    cout<<"Enter weight in pounds :";
    cin>>p;
    kg=  convertweight(p);
    cout<<"KG = "<<kg;

    cout<<"\nEnter weight in pounds :";
    cin>>p;
    kg=  convertweight(p);
    cout<<"KG = "<<kg;

    cout<<"\nEnter weight in pounds :";
    cin>>p;
    kg=  convertweight(p);
    cout<<"KG = "<<kg;

}

Note: inline keyword is actually just a request to the compiler. Sometimes the compiler will ignore the request and compile the function as a normal function. It might decide the function is too long to be inline,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s