Public Group

# what's the big difference between a function and a macro?

This topic is 4932 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

It's something I wondered about for quite some time. Why do people write (small) functions using a macro instead of just writing a "normal" function? What exactly is the benefit?

##### Share on other sites
None to little, don't do it. It opens your code up to all kinds of funny little bugs that will make you lose your hair prematurely.

##### Share on other sites
A function is called as a procedure, so you can have it in a library file. You can't do that with a macro, as all a macro does is replace x with whatever the function does. Also, macros can sometimes make rather ugly code... especially that Allegro END_OF_MAIN thing.

##### Share on other sites
[whining]but I wanna knooooowww[/whining]

So what's the point of having macros? They must have been useful for something.

##### Share on other sites
I think it the macro and all the '#'s gets done before the code is written into the exe ( or compiled ) ... this might be useful if you have some kind of math that you need an awnser from.

MUst sLeEP

##### Share on other sites
A function has to push and pop off of the stack and takes a performance penalty when called (extremely small and not important in 99.9999999% of cases). A macro is like an inline function - the code gets inserted directly each place you use it when compiled which is a bit faster. It's really only useful in the critical loops of your code that get called thousands of times every frame.

##### Share on other sites
Macros also let you do some hacky stuff you probably shouldn't be doing in the first place.

Macros are also static (compile-time) meaning, if you're clever, you can do some things you otherwise couldn't.

For example, Boost's STATIC_ASSERT macro allows you to check a condition at compile-time. That couldn't be done with an inline function (or a function at all).

##### Share on other sites
Well using a macro has just saved me from writing the same bit of code in many classes:

class GameActor : public GameEntity{public:    GameActor(){}    ~GameActor(){}    char name[128];    int  controller;    int  state;    char model[256];private:    DECLARE_HEAP;};

And DECLARE_HEAP is defined as:
#define DECLARE_HEAP '\'     public: '\'         static void * operator new(size_t size); '\'         static void operator delete(void * p, size_t size); '\'     private: '\'         static Heap * s_pHeap; '\'

('\' is without the 's, but they keep geting removed????)

The macro allows me to write the code one and put in in any class that I want to overload new and delete in. Ohhh...there is a corresponding DEFINE_HEAP as well..:)
although I could (and should) probably use templates but templates frighten me a little (ohhhhh, the big bad template is gonna get me), coz I don't really know how to use them properly.

And macros don't exactly promote type saftey now, and probably many other things besides. I imagine Fruny will have a complete explination..;)

Oh and marcos are part of pre-compilation not regular, run of the mill, compilation.

##### Share on other sites
Quote:
 Original post by Shai[whining]but I wanna knooooowww[/whining]So what's the point of having macros? They must have been useful for something.

Macros became popular in the wee olden' days of programming. They are (were) a convenient way for programmers (and especially compiler writers) to add powerful features to languages. A text pre-processor is much easier to make than a compiler allowing for the same features. Generally modern languages and sophisticated compiler features have made macros less convenient and less necessary. Of course, macros can still do things that are otherwise difficult and unnecessarily complex.

##### Share on other sites
Have a browse around the MFC source, or the old Half Life 1 SDK, if you can't get your hands on the HL2 SDK. Loads of macros, usually doing things that just can't be done with functions, makes life WAY easier and code MUCH easier to read/write.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 34
• 16
• 11
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015651
×