Inline Functions Similar to Macros F unction code is expanded at the point of function call at compile time. Parsed by the compiler. Inline functions follow all the protocols of type safety enforced on normal functions. Expressions passed as arguments to inline functions are evaluated once.
Inline Functions It is declared by using keyword “ inline” before the function prototype. Argument types are checked and necessary conversions are performed correctly. The compiler performs return type checking, function signature before putting inline functions into the symbol table. They can be overloaded to perform the right kind of operation for the right kind of data.
Inline Functions Can be used for debugging a program as they are expanded at compile time and a break point can be placed at the inline function definition. We can do step by step debugging. Inline functions can access class’s member data. Inline functions may or may not be expanded by the compiler. It can be defined inside or outside the class. Inline functions pass arguments by value., just like regular functions.
Inline functions Vs Regular functions The difference between an inline function and a regular function is that whenever the compiler finds a call to an inline function, it writes a copy of the compiled function definition. With a regular function, a normal function call is generated. Inline functions looks like regular functions.
Macros Preprocessor directive provided by C. Declared by #define . Macros are expanded by preprocessor at precompile time. Expressions passed are arguments are evaluated more than once. Good for declaring constants. Provide textual substitution. Each time the macro name is encountered with arguments, the arguments used in its definition are replaced by the actual arguments found.
Macros Macros are more error prone as compared to inline functions. The parameters are not typed. The macro works for any objects of arithmetic type. No error checking is done during compilation. e.g.. You can pass strings to a macro that does some integer arithmetic. Cannot be used for debugging as they are expanded at pre-compile time.
Macros The preprocessor has no permission to access member data of a class and are thus useless even if used within a class. Thus they cannot even be used as member functions. Macros are always expanded. It cannot be defined inside the class. Macros don’t pass by value. Expressions passed into macros are not always evaluated before entering the macro body.
Macros Expressions may expand inside the macro so that their evaluation precedence is different from what you expect. C++ preprocessor implements macros by using simple textual replacements.
Difference between Macros and Inline functions Inline functions take defined type arguments whereas Macros can take any type as arguments. Macros are expanded by the preprocessor and then compilation takes place. Macros are expanded by C preprocessor. Inline functions are parsed by the compiler. The program size increases with both macros and inline functions.
Sample code. Copy and paste it in any C/C++ Editor to see results. #include< stdio.h > #define SUM(x) x*x #define SUM1(x) (x)*(x) #define SUM2(x) x++ * x++ #define SUM3(x) (x++)*(x++) #define toupper (a) ((a) >= 'a' && ((a) <= 'z')?((a)-(' a'-'A ')):(a)) #define max( c,d ) (c> d?c:d ) #define max1( a,b ) ((a<b)?b:a) int main() { int y=3; int z=10; char ch ; int f=1,e=2; printf ("\r\ nhello world!"); printf ("\r\ n%d",y ); int j= SUM(y); printf ("\r\ n%d",j ); printf ("\r\n");