top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Difference Between Macro and Function

0 votes
411 views

Difference Between Macro and Function This is not just applicable to C but all languages where we have a concept of preprocessing.

  1. Macro is preprocessed execution i.e. macro processing takes place before compilation whereas function is compiled in short for macro before compilation macro name is replaced by macro value but for the function call, Transfer of Control takes place

  2. For macro no typechecking is done where as for function compiler do the type checking.

  3. Since macro is preprocessed and code is replaced before compilation so code length Increases in Macro which is not applicable to function.

  4. Improper use of macro can lead to side effect see the following example
    #define square(x) x*x
    Now when we call square(2+5) it is replaced with (2+5*2+5) i.e. 17 in place of 49

  5. Macro is preprocessor but at run time macro provide better speed.

  6. Macro does not Check Compile Errors where as Function Checks.

  7. My Interpretation - Macro is useful for small but reusage code but function is useful for large code.

posted Jul 10, 2014 by Salil Agrawal

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button

...