Difference Between Macro and Inline Function

A macro is a fragment of code, which is a preprocessor directive. An inline function is a C++ enhancement feature to minimize the execution time of a program. Therefore, the key difference between Macro and Inline Function is that a macro is checked by the preprocessor while an inline function is checked by the compiler.

A macro is included at the beginning of the program preceded by a hash sign. When there is the macro name in the program, it is replaced by the content of the macro.

CONTENTS

1. Overview and Key Difference
2. What is Macro
3. What is Inline Function
4. Side by Side Comparison – Macro vs Inline Function in Tabular Form
5. Summary

What is Macro?

The preprocessor is a program that processes the source code before it passes through the compiler. It operates using the preprocessor command line or the directives. In the program, the preprocessor directives are placed in the source program before the main program. Before the source code goes through the compiler,  it is checked by the preprocessor for preprocessor directives. The preprocessor directives have the # symbol. Unlike other statements, they do not end with a semicolon. One type of preprocessor directive is macro. Generally, the macros are written in capital letters.

Figure 01: C++ Program with Macros

According to the above program, line 3 and line 4 indicates macros. When calculating the area, the PI value is substituted using the defined macro. In line 14, volume= CUBE(value), the preprocessor expands the statement as volume = (value * value * value). Finding the cube can be written as a function, but in here it is written using a macro. If there is a statement as volume = CUBE(x+y), then it will expand to volume = (x+y * x+y * x+y).

Some programming tokens might easily be mistyped. They can be replaced using macros. e.g. #define AND  &&, #define  OR  ||. A macro definition can also include expressions such as #define AREA  4*5.56.

What is Inline Function?

When a function is called, the compiler takes some time to execute it. If the function is not very complex, the programmer can convert the function to an inline function.  Refer the below program.

Figure 02: Function without Inline

The print_hello is a simple function. It prints the string “Hello” when the function is called.  The execution time for that function is 0.187s. When using the inline keyword as follows, the execution time reduces to 0.064s.

Figure 03: Inline Function

Therefore, by using the inline keyword, the execution time reduces. The inline functions might  not work if there are loops, switch statements and if the function contains static variables or recursive functions.

What is the Difference Between Macro and Inline Function?

Macro vs Inline Function

A macro is a fragment of code, which is a preprocessor directive that is included at the beginning of the program preceded by a hash sign. An inline function is a C++ enhancement feature to minimize the execution time of a program.
 Evaluation Time
In macro, the argument is evaluated each time it is used in the program. In inline, the argument is evaluated once.
Checked By
A macro is checked by the preprocessor. An inline function is checked by the compiler.
 Keyword
Marco uses #define. The inline function uses the keyword ‘inline’.
Usage
Macro can be used to define constants, expressions, for literal text substitution and to define functions etc. An inline function can be used to minimize the execution time of the program.
Termination
Macro terminates with the new line. Inline function terminates with the curly brace at the end of the inline function.
Defining Point
A Marco is defined at the beginning of the program. An inline function can be inside or outside the class.

Summary – Macro vs Inline Function

This article discussed the difference between Macro and Inline Function. These concepts are used in C++ programming. The difference between Macro and Inline Function is that a macro is checked by the preprocessor while an inline function is checked by the compiler.

Reference:

1.“Preprocessor Directives.” Cplusplus.com. Available here 
2.“C Inline Functions.” Www.tutorialspoint.com, Tutorials Point, 19 Mar. 2018. Available here