Follow + Star “Embedded Development Notes”, so you won’t miss out on exciting content!
Friends who often write code should have encountered inline functions and macro definitions. These two types of writing are frequently seen in many codes, especially in large codebases where strictness and standardization are required, allowing one to learn many clever techniques from them.
This article compares inline functions and macros, providing a simple analysis of their usage and differences.
01 Inline Functions
Inline functions are a function optimization mechanism in C++, introduced in C99.
To use them, you suggest the compiler to directly insert the function body at the call site by adding the inline keyword before the function declaration, instead of performing a conventional function call.
This means that at the location where the inline function is called in the code, it does not simply return a result from a function call; rather, the entire code snippet of the inline function is directly inserted into the code for execution.
See the code below:
// Define inline function, declared with inline
inline int max(int a, int b) { return a > b ? a : b;}
int main() { int x = 5, y = 10; int m = max(x, y); // Will be replaced with int m = x > y ? x : y; return 0;}
In the above code, the inline function max() does not simply return a result; it requires the code segment to be directly inserted into the code, effectively expanding the code before execution.
Compared to regular functions, inline functions can speed up program execution by reducing the overhead of function calls, as they are embedded into the target code during compilation.
02 Macro Functions
Macro functions are a text replacement mechanism defined using the preprocessor #define directive, which is expanded by the preprocessor before compilation.
In simple terms, macro functions are purely text replacements, limited to the function of macros themselves, merely replacing text. However, they are still very useful for code cleanliness and clarity, especially when the same code text is used multiple times in the code; using macros for replacement is a good method.
See the definition of a macro function below:
(1) Simple single-statement macro function:
// Simple max macro
#define MAX(a, b) ((a) > (b) ? (a) : (b))
(2) Definition of a multi-statement macro function:
// Multi-statement macro
#define SWAP(a, b) do {
temp = a;
a = b;
b = temp;
} while(0)
Note: When writing multi-statement macro functions across multiple lines, you need to use ‘\’ as a line continuation character; otherwise, it will result in an error.03 Advantages and Disadvantages of Inline Functions and Macros(1)Inline functions perform parameter type checking, while macro functions do not check the types of parameters and return values, even if the parameters are incorrect. For example, the following macro function:
MAX(6, "Handsome Guy")
will not notify you of the error.(2) Inline functions are directly embedded in the code but are recommended to be short; they should not be too long. Compared to macro functions, inline functions are safer and more reliable, but this comes at the cost of increased space (the executable file will be larger).(3) Improper use of macro functions may lead to ambiguity issues, which inline functions do not have.(4) When issues arise in the code, inline functions can be debugged like normal functions, while macro functions do not support debugging; you only see the expanded code.(5) Inline functions are processed during compilation and may be optimized by the compiler, while macro functions are expanded during the preprocessing stage, merely performing text replacement without compiler optimization.(6) The inline request for inline functions is merely a suggestion; the compiler may ignore inline, while macro functions will always be replaced without worrying about compiler optimization issues being overlooked.(7) Inline functions are essentially functions but cannot contain static variables or be recursively called; macro functions do not have the concept of scope and are globally visible.
If you find this article helpful, please give it a follow! 
Please give a recommendation! 