CPP Inline Function

What is Inline Functions?

  • A small performance overhead occurs in jumping in and out of functions.
  • The main reason to use inline function is to increase speed.
  • If a function is declared/prototyped 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 Function example:
    Source code Result
    #include <iostream>
    // Prototyping of the function
    inline double Convert(double);
    // or
    // double Convert(double tFahrenheit);
    int main()
      using namespace std;
      double tFahrenheit;
      double tCelsius;
      cout << "Temperature in Fahrenheit: ";
      cin >> tFahrenheit;
      // The function code will be copied to this place
      tCelsius = Convert(tFahrenheit);
      cout << "Temperature in Celsius is: ";
      cout << tCelsius << endl;
      return 0;
    // Implementation of the function  
    double Convert(double tFahrenheit) {
      return ((tFahrenheit - 32) * 5) / 9;
    Temperature in Fahrenheit: 68
    Temperature in Celsius is: 20

    You could may be losing speed instead of increasing speed using inline function if the function is large. This is because the increased size of the executable program might in fact actually slow down the program.

    You can download this example here (needed tools can be found in the right menu on this page).

© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.