Difference Between Macros and Inline Functions

Inline function is an important addition in C++. These inline functions mostly are not called and is expanded in line at the invocation place. Hence, these functions are called inline functions. To define a function as inline function, precede function definition with “inline” keyword. Check inline function for more information.

Macros are preprocessor directive which simply substitute patterns in the code. They can be used anywhere in the code, and these are expanded before compilation begins.

MacrosInline Function
Macros are defined using #define keyword. Eg: #define TEST_MACRO 10To define a function as inline function, precede function definition with “inline” keyword.
Since Macros are expanded before compilation, debugging becomes difficult.Inline function can be easily debugged.
Macros can be used in other macros.Inline functions don’t follow this.
Macros are always expanded.Inline functions are recommendation to compiler which can be ignored.
Macros are not type safe and it simply substitute patterns whether they are syntacticallycorrect or not.Inline function is a normal function in almost all aspect.
Macros doesn’t have any scope for variables, and it uses the scope in which macro is being used.Inline function has its own scope for the variables.

Leave a Reply

Your email address will not be published. Required fields are marked *