C++ Inline Functions

An inline function is a function that is expended in line when it is invoked, that is the compiler replaces the function call with the corresponding function code.

It is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

***** Remember that inline keyword merely sends the request, not a command to the compiler, the compiler may ignore this request if the function definition is too long or too complicated and compile the function as a normal function.****

Advantages

  • It avoids the overhead of calling the actual function. This is because the complier performs and inline expansion which eliminates the time overhead when a function is called.
  • Reduces space as no separate set of instructions in memory is written.
  • Using inline, you can put a function definition in a header file (i.e. it can be included in multiple compilation unit, without the linker complaining.)

Some situations where inline expansion may not work, they are:

  • If function returning some values and a loop, a switch or a goto exists.
  • If function return type is void and a return statement is exists.
  • If function contains any static variable.
  • If inline function is recursive.

Syntax

//Syntax to declare and define a function
inline return_type function_name(argument_list)
{
    ....;
    ....;
    ....;
    [return value;]
}
Advertisement

Consider the example

#include <iostream>
using namespace std;

// Program to create functions using inline...
inline double square(int n)
{
	return n*n;
}

inline float average(int n1,int n2)
{
	return ( (float)(n1+n2)/2 );
}

int main()
{
	cout<<"\n SQUARE IS  = "<< square(12);
	cout<<"\n AVERAGE IS = "<< average(10,21)<<"\n";
	return 0;
}
SQUARE IS  = 144
AVERAGE IS = 15.5

Inline functions with in the class

A member function can also be declared as inline function, use the inline keyword before function declaration and definition, if function definition is written outside of class.

Consider the example

// Program to create functions using inline with in the class...
#include <iostream>
using namespace std;

class Number
{
	private:
		int num;
	public:
		void getNumber(void);
		inline double square(void);
		inline double cube(void);
};

// defining these functions
void Number::getNumber (void)
{
	cout<<"Enter an integer number :";
	cin>>num;
}
inline double Number::square (void)
{
	return num*num;
}

inline double Number::cube(void)
{
	return num*num*num;
}

int main()
{
	Number objN;
	objN.getNumber();
	cout<<"\n SQUARE IS ="<< objN.square ();
	cout<<"\n CUBE IS ="<< objN.cube ();
	cout<< endl;
	return 0;
}
Enter an integer number :11
SQUARE IS =121
CUBE IS =1331

Related Tutorials

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT


Top MCQs

Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.