Inline functions
- Are a C++ enhancement designed to speed up programs
The primary distinction between normal functions and inline functions
- Is not in how you code them
- But in how the C++ compiler incorporates them into a program
To understand the distinction between inline functions and normal functions
- You need to peer more deeply into a program’s innards than we have so far
- Let’s do that now
The final product of the compilation process
- Is an executable program, which consists of a set of machine language instructions
- When you start a program, the operating system loads these instructions into the computer’s memory so that each instruction has a particular memory address
- The computer then goes through these instructions step-by-step
- When you start a program, the operating system loads these instructions into the computer’s memory so that each instruction has a particular memory address
Sometimes, as when you have a loop or a branching statement
- Program execution skips over instructions, jumping backward or forward to a particular address
Normal function calls
- Also involve having a program jump to another address (the function’s address)
- And then jump back when the function terminates
Let’s look at a typical implementation of that process in a little more detail
- When a program reaches the function call instruction
- The program stores the memory address of the instruction immediately following the function call
- Copies function arguments to the stack (a block of memory reserved for that purpose)
- Jumps to the memory location that marks the beginning of the function
- Executes the function code (perhaps placing a return value in a register)
- And then jumps back to the instruction whose address it saved
- Executes the function code (perhaps placing a return value in a register)
- Jumps to the memory location that marks the beginning of the function
- Copies function arguments to the stack (a block of memory reserved for that purpose)
- The program stores the memory address of the instruction immediately following the function call
Jumping back and forth and keeping track of where to jump
- Means that there is an overhead in elapsed time to using functions
C++ inline functions
- Provide an alternative
In an inline function
- The compiled code is “in line” with the other code in the program
- That is, the compiler replaces the function call with the corresponding function code
With inline code
- The program doesn’t have to jump to another location to execute the code and then jump back
- Inline functions thus run a little faster than regular functions
- But they come with a memory penalty
- Inline functions thus run a little faster than regular functions
If a program calls an inline function at 10 separate locations
- Then the program winds up with 10 copies of the function inserted into the code
- See (Image-1)
(Image-1) Inline functions versus regular functions

You should be selective about using inline functions
- If the time needed to execute the function code is long compared to the time needed to handle the function call mechanism
- Then the time saved is a relatively small portion of the entire process
- If the code execution time is short
- Then an inline call can save a large portion of the time used by the non-inline call
- On the other hand, you are now saving a large portion of a relatively quick process, so the absolute time savings may not be that great unless the function is called frequently
To use this feature, you must take at least one of two actions:
- Preface the function declaration with the keyword inline
- Preface the function definition with the keyword inline
A common practice is
- To omit the prototype
- And to place the entire definition (meaning the function header and all the function code) where the prototype would normally go
The compiler does not have to honor your request to make a function inline
- It might decide the function is too large
- Or notice that it calls itself (recursion is not allowed or indeed possible for inline functions)
- Or the feature might not be turned on or implemented for your particular compiler
- Or notice that it calls itself (recursion is not allowed or indeed possible for inline functions)
(A-1) illustrates the inline technique with an inline square() function that squares its argument
- Note that placed the entire definition is on one line
- That’s not required, but if the definition doesn’t fit on one line, the function is probably a poor candidate for an inline function
Example of (A-1)
// inline.cpp -- using an inline function #include <iostream> // an inline function definition inline double square(double x) { return x * x; } int main() { using namespace std; double a, b; double c = 13.0; a = square(5.0); b = square(4.5 + 7.5); // can pass expressions cout << "a = " << a << ", b = " << b << "\n"; cout << "c = " << c; cout << ", c squared = " << square(c++) << "\n"; cout << "Now c = " << c << "\n"; return 0; }
Here’s the output of the program in (A-1):

This output illustrates that inline functions pass arguments by value just like regular functions do
- If the argument is an expression, such as 4.5 + 7.5, the function passes the value of the expression—12 in this case
- This makes C++’s inline facility far superior to C’s macro definitions
- See the “Inline Versus Macros”
- This makes C++’s inline facility far superior to C’s macro definitions
Even though the program doesn’t provide a separate prototype, C++’s prototyping features are still in play
- That’s because the entire definition, which comes before the function’s first use, serves as a prototype
- This means you can use square() with an int argument or a long argument
- And the program automatically type casts the value to type double before passing it to the function
- This means you can use square() with an int argument or a long argument
Inline Versus Macros
The inline facility is an addition to C++
- C uses the preprocessor #define statement to provide macros
- Which are crude implementations of inline code
- For example, here’s a macro for squaring a number (A-2):
- Which are crude implementations of inline code
Example of (A-2)
#define SQUARE(X) X*X
(A-2) this works not by passing arguments but through text substitution
- With the X acting as a symbolic label for the “argument” (A-3):
Example of (A-3)
a = SQUARE(5.0); // is replaced by a = 5.0*5.0; b = SQUARE(4.5 + 7.5); // is replaced by b = 4.5 + 7.5 * 4.5 + 7.5; d = SQUARE(c++); // is replaced by d = c++*c++;
(A-3) only the first example here works properly
- You can improve matters with a liberal application of parentheses (A-4):
Example of (A-4)
#define SQUARE(X) ((X)*(X))
Still, the problem remains that macros don’t pass by value
- Even with this new definition, SQUARE(c++) increments c twice
- But the inline square() function in (A-1) evaluates c, passes that value to be squared, and then increments c once
The intent here is not to show you how to write C macros
- Rather, it is to suggest that if you have been using C macros to perform function-like services
- You should consider converting them to C++ inline functions

Discussion
No comments for “C++ Inline Functions”