Chaitu Tech Bits

what is difference between macro and function? what is macro and what is funcution

Wednesday, December 15, 2010


1.Difference between a macro and a function is that a macro returns code and a function returns a     value.
2.  macro 'calls' nearly always get expanded once at compile time wheras most  function calls are evaluated many many times.
           
            When you call a function your compiler enters a call-sequence (which takes
            time) and allocates a new stack frame for that function (whcih takes text
            stack space) so that the function's body can be executed. After it's done
            you enter a returning-sequence phase (which takes time).

            A macro does not need anything of the above, because it's preprocessor's job
            to expand a macro, it's only about text replacement, not about compiler
            stuff or code-generating issues. So you don't expend time and space doing
            what a function would need in order to be executed.

            That makes functions and macros[/b] completely different even if the result of
            using both is, in some cases, the same. One must know it's suitable to use
            one or the other. For instance: you can't point a pointer to a macro, but
            you can use pointers to functions...


Share/Bookmark

Related Posts Plugin for WordPress, Blogger...