By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Function as macro parameter

P: n/a
Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!
Nov 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andrew wrote:
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!


Some people think the code looks better if there is return after each
function. (So they can see the end of a function right away.)

No other benefits than that.

Nov 13 '05 #2

P: n/a
an***********@ca.com (Andrew) wrote:
Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function?
A macro is handled by the C preprocessor. The C preprocessor knows
nothing about machine code or pointers - almost all it does is pure
text replacement (well, outside string literals, that is).
Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!


You do not gain much in placing a return statement at the end of
a function returning void, execpt perhaps better readability -
it depends mostly on presonal preference.
--
6 * 9 = 42 (base 13)
Nov 13 '05 #3

P: n/a
Andrew wrote:
Hello,

Is it bad practice to use a function as a parameter of a macro? Will
the compiler create a full copy of the function's machine code for
each invocation of the macro? Or does it create some type of pointer
to the function? Also is there any benefit from placing a return at
the end of a function which returns void? Thanks for any advice!


Macros are not real functions in that they are completely removed before
your code even makes it to the compiler.

Calling a macro is identical to if you placed the body of the macro at
the macro call site, so no 'copy' of the function is ever made.

PS. Even passing a function as a parameter into a normal function
doesn't incur any copy overhead. Referencing a function by name simply
gives you a pointer to it, so you are essentially passing only a pointer
to the function.
--
Paul Richards

Sun Microsystems, Advanced Development
New Technology Researcher

Nov 13 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.