What Is an Inline Function and How Is It Different from a Macro?


inline function

*This post may contain affiliate links. As an Amazon Associate we earn from qualifying purchases.

During your IT interview, you may be asked what is an inline function in C++ and how it is different from a macro function. Let us tell you the most important things you need to know to provide a complete answer to this common question.

What is an inline function in C++?

Inline functions?in C++ are essentially enhancement features used to increase a program?s execution time. They can be used in compilers to make these inline. Then the compiler is able to replace these function definitions each time they are called. Inline functions create a copy of the compiled function definition.

The main thing you need to remember is that they enable compilers to replace the definition of inline functions at compile time, as opposed to referring the function definition at run time.

The Advantages of Inline Functions

  • They?essentially speed up the program because they avoid function calling overhand.
  • They?can increase reference locality by making use of an instruction cache.
  • Users can put function definitions in header files via inline function, which means that it can be used multiple compilation units.

The Disadvantages of Inline Functions

  • They?increase the executable size because they expand the code.
  • Because they replace definitions at compile time, changing the code of the inline function is very lucrative. You will have to recompile the entire code to make sure it is properly updated.
  • Since they increase executable size, this could cause damage to the memory.

What is the main difference between an inline function and a macro function?

There are some similarities between macro?functions and inlines, but they are not quite the same thing. Macros are typically fragments of code which are provided with a name. Then, each time the name is used, it is essentially replaced with the code included in the macro.

If the inline can create a copy of the function definition, the macro function replaces the code exactly as it is identified and defined in the function. Therefore, they do not have the same attributes.

Image Source:?Makerfaireoslo

Recent Posts