C++ Inline Functions

By IncludeHelp Last updated : November 04, 2023

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. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

Note

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.

Create an inline function

To create an inline function, simply prepend inline keyword to function prototype to make a function inline.

Syntax

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

Call an inline function

To call an inline function, write the function's name followed by two parentheses () along with the parameters (if any) and a semicolon ;.

Syntax

functio_name();    

Example

In the below example, square() and average() are the inline functions.

// Program to create functions using inline

#include <iostream>
using namespace std;

// inline function
inline double square(int n) { return n * n; }

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

// main code
int main() {
  // calling inline functions
  cout << "SQUARE IS  = " << square(12) << endl;
  cout << "AVERAGE IS = " << average(10, 21) << endl;
  return 0;
}

The output of the above program will be:

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.

Example

In the below example, square() and average() are the inline functions inside the class (with in the class).

// Program to create functions using inline
// with in the class

#include <iostream>
using namespace std;

// Define a class
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;
}

// iniline functions definitions
inline double Number::square(void) { return num * num; }
inline double Number::cube(void) { return num * num * num; }

// main function
int main() {
  // creating object
  Number objN;

  // calling member function
  objN.getNumber();

  // calling inline member functions
  cout << "SQUARE IS =" << objN.square() << endl;
  cout << "CUBE IS =" << objN.cube() << endl;

  return 0;
}

The output of the above program will be:

Enter an integer number :11
SQUARE IS =121
CUBE IS =1331

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.)

Situations where inline expansion may not work

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.

Related Tutorials

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.