INLINE FUNCTIONS & MACROS
Designed by,
Anand K
InlineFunctions
 Similar to Macros
 Function 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.
InlineFunctions
 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.
InlineFunctions
 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.
InlinefunctionsVsRegularfunctions
 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.
DifferencebetweenMacrosandInline
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.
Samplecode.Copyandpasteit in any
C/C++ Editortoseeresults.
 #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("rnhello world!");
 printf("rn%d",y);
 int j= SUM(y);
 printf("rn%d",j);
 printf("rn");
 printf("rn%d",y);
 int k= SUM1(y);
 printf("rn%d",k);
 printf("rn");
 printf("rn%d",y);
 int i= SUM(++y);
 printf("rn%d",i);
 printf("rn");
 printf("rn%d",y);
 int a= SUM(y++);
 printf("rn%d",a);
 printf("rn");
 printf("rn%d",y);
 int l= SUM2(y);
 printf("rn%d",l);
 printf("rn");
 printf("rn%d",y);
 int m= SUM3(y);
 printf("rn%d",m);
 printf("rn");
 int g=max(f++,e++);
 printf("rn%d,%drn",f,e);
 printf("rnMax of 10 and 30 isrn");
 int h=max1(41,40);
 printf("rn%drn",h);
 printf("rnEnter a character:rn");
 ch=toupper(getc(stdin));
 printf("rn%c",ch);
 getch();
 return 0;
 }

Inline functions & macros

  • 1.
    INLINE FUNCTIONS &MACROS Designed by, Anand K
  • 2.
    InlineFunctions  Similar toMacros  Function 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.
  • 3.
    InlineFunctions  It isdeclared 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.
  • 4.
    InlineFunctions  Can beused 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.
  • 5.
    InlinefunctionsVsRegularfunctions  The differencebetween 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.
  • 6.
    Macros  Preprocessor directiveprovided 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.
  • 7.
    Macros  Macros aremore 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.
  • 8.
    Macros  The preprocessorhas 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.
  • 9.
    Macros  Expressions mayexpand inside the macro so that their evaluation precedence is different from what you expect.  C++ preprocessor implements macros by using simple textual replacements.
  • 10.
    DifferencebetweenMacrosandInline functions  Inline functionstake 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.
  • 11.
    Samplecode.Copyandpasteit in any C/C++Editortoseeresults.  #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("rnhello world!");  printf("rn%d",y);  int j= SUM(y);  printf("rn%d",j);  printf("rn");
  • 12.
     printf("rn%d",y);  intk= SUM1(y);  printf("rn%d",k);  printf("rn");  printf("rn%d",y);  int i= SUM(++y);  printf("rn%d",i);  printf("rn");  printf("rn%d",y);  int a= SUM(y++);  printf("rn%d",a);  printf("rn");  printf("rn%d",y);  int l= SUM2(y);  printf("rn%d",l);  printf("rn");  printf("rn%d",y);  int m= SUM3(y);  printf("rn%d",m);  printf("rn");
  • 13.
     int g=max(f++,e++); printf("rn%d,%drn",f,e);  printf("rnMax of 10 and 30 isrn");  int h=max1(41,40);  printf("rn%drn",h);  printf("rnEnter a character:rn");  ch=toupper(getc(stdin));  printf("rn%c",ch);  getch();  return 0;  }