Is it always better to write functions over macros for even smaller
things, say square of a number ?
It is always better to write the code in a way that best (tm)
satisfies several possibly competing requirements.
First and foremost, it must do the job.
It should be easily maintained.
It should be easy to read and understand
It should be extendable (for example, can the "arguments" be
expressions with side effects).
It should promote re-use.
For my project, I have a file called util.h which basically contains
some math macros like this, error display macro, constants etc. all at
one place and its a few lines anyway so including it in most files
won't hurt that bad whereas if i write full fledge functions it
A lot of projects are done this way so it can't be that unreasonable.
increases the amount of code that is included everytime. Is that
correct ?
If you invoke a macro 100 times will it generate more code than if you
call the same function 100 times? As always, it depends.
If you find a problem with the macro, how many routines will need to
be recompiled? If you fix the function, how many?
You are on the verge of treating the issue as a premature
optimization. In the vast majority of cases, this places the emphasis
on the wrong syllable. Write clear working code and don't worry about
extra lines of code in the compile phase or a few extra K of memory in
the execution phase.
Remove del for email