본문 바로가기

Development/C++

Inline function #3

C++ Inline Functions

In this tutorial, we will learn about inline functions in C++ and how to use them with the help of examples.

In C++, we can declare a function as inline. This copies the function to the location of the function call in compile-time and may make the program execution faster.

Before following this tutorial, be sure to visit the C++ Functions.


Inline Functions

To create an inline function, we use the inline keyword. For example,

inline returnType functionName(parameters) {
    // code
}

Notice the use of keyword inline before the function definition.


C++ Inline Function

#include <iostream>
using namespace std;

inline void displayNum(int num) {
    cout << num << endl;
}

int main() {
    // first function call
    displayNum(5);

    // second function call
    displayNum(8);

    // third function call
    displayNum(666);

    return 0;
}

Output

5
8
666

Here is how this program works:

Working of inline functions in C++

Here, we created an inline function named displayNum() that takes a single integer as a parameter.

We then called the function 3 times in the main() function with different arguments. Each time displayNum() is called, the compiler copies the code of the function to that call location.

 

 

 

 

 

 

 

https://archive.ph/ooXhr

https://www.programiz.com/cpp-programming/inline-function

 

C++ Inline Functions

In C++, we can declare a function as inline. This copies the function to the location of the function call in compile-time and may make the program execution faster. Before following this tutorial, be sure to visit the C++ Functions. Inline Functions To cr

www.programiz.com

 

'Development > C++' 카테고리의 다른 글

ASCII code table  (0) 2022.03.27
C++ Pass By Reference Explained [중요]  (0) 2022.03.26
Inline Funtion #2 - Syntax, Examples.  (0) 2022.03.26
Inline function #1  (0) 2022.03.26
(const MyClass& object) VS (MyClass& object)  (0) 2022.03.26