---
title: "C++ Inline Functions Tutorial"
url: https://weworkworldwide.com/tutorials/inline-functions-2/
description: "C++ Inline Functions Tutorial: When a program is running, the computer will go through each of instructions within that program and start to execute them."
date: 2026-10-06T13:00:29+00:00
source: https://weworkworldwide.com/llms.txt
---

# C++ Inline Functions Tutorial

“When I let go of what I am, I become what I might be” Lao Tzu

When a program is running, the computer will go through each of instructions within that program and start to execute them. But the thing is, each function of a program is stored in a different place in the memory and if for example a function named `fB()` is called in the body of another function named `fA()`, the computer needs to jump from `fA()` function and go to the location that the `fB()` is stored and start to run the instructions in that place.

After that, the computer again needs to jump back to the memory location that the `fA()` function is stored and keep running the rest of instructions in that function.

Moving back and forth and keeping track of where to jump can result of wasting the CPU’s time.

This is where `inline function` can be helpful.

If a function is turned into an inline-function, it’s a signal to the compiler that is should find any call to this function and replace the function-call with its actual body. This way when the computer is running the program, there’s no need to jump to another memory location in order to run a function.

To turn a normal function into an inline-function we must at least do one of these two actions:

-   Preface the function declaration with the keyword  
    `inline`.

-   Preface the function definition with the keyword  
    `inline`.

Example:

``` line-numbers
#include <iostream>
 using namespace std;
 inline void printName();
 int main() {
 
 printName();
 printName();
 printName();
 return 0;
 }
 void printName(){
 cout<<"My name is John Doen";
 }
```

In this example we’ve turn the `printName()` function into an inline function. So at compile time, the compiler will check any call to the `printName()` function and will replace that call with the body of the function itself.

So at compile time the source-code above, will be turned into this:

``` line-numbers
#include <iostream>
 using namespace std;
 inline void printName();
 int main() {
 
 cout<<"My name is John Doen";
 cout<<"My name is John Doen";
 cout<<"My name is John Doen";
 
 return 0;
 }
 void printName(){
 cout<<"My name is John Doen";
 }
```

Then of course at runtime we will get this output:

## My name is John Doe

## My name is John Doe

Note: The compiler might not accept our request to make a function inline. This is because the function might be too large or it calls itself (recursion is not allowed for inline functions).
