MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Macros vs Functions


Feature
Macros
Functions
Definition
Defined using #define
Defined using the return_type function_name(parameters) syntax
Compilation
Processed by the preprocessor before compilation
Processed by the compiler during compilation
Substitution
Performs textual substitution
Executes a block of code
Evaluation
Substituted as-is, no type checking
Type-checked with arguments evaluated at runtime
Arguments
Arguments are not evaluated; they are substituted directly into the code
Arguments are evaluated before function call
Performance
Inline expansion can be faster but may increase code size
Function call overhead can be higher but usually provides better optimization
Debugging
Can be harder to debug as macros are expanded by the preprocessor
Easier to debug with stack traces and function names
Side Effects
Can lead to unexpected results if arguments have side effects
Side effects are controlled within function scope
Overloading
Not supported; each macro must have a unique name
Function overloading (in C++) allows multiple functions with the same name but different signatures
Recursion
Not directly supported; requires creative use of macros
Supported with standard function call mechanisms
Memory
No memory management involved; code size can increase
Memory managed via stack; can have local variables and return values


Report an issue

Reporting: Macros vs Functions (topic)

Related Posts