dcsimg
Results 1 to 8 of 8

Thread: Function vs Macro

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    180

    Function vs Macro

    For curiosity only.

    What is the difference between a Function and a Macro?

    C++ has some macros MAKELONG macro for example.

    To me it is a function -> something IN, something else OUT

    I always thought of a Macro as something where for example where you would record keystrokes with SendKeys

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,504

    Re: Function vs Macro

    It's been a while, but I think this is how I remember it...
    It's a different kind of macro. The kind of macro you're thinking of is the kind that's in Office... where a key combination is tied to a piece of code of some kind.
    A macro in C++ is a bit different. It's a code macro. Actually, they are analogous to each other... a macro is a piece of code that's tied to a shortcut invoker of some kind. In Office, that invoker is a key combination, in C/C++ it's a NAME. When you create a macro, you give it a name, then when you use that name in the code, that gets replaced with that piece of code... it doesn't run that code - and that's where it is different from a function - but it copies that code into that spot. It's also possible (if I remember right) to create a macro that allows parameter input that then modifies the macro output (in which case, it's similar to a function) but it still doesn't actually execute that macro, it simply replaces the place holder with the definition of the macro.



    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,646

    Re: Function vs Macro

    Some language implementations are more powerful than others too. In some cases the compiler or preprocessor accepts a more complete version of the language syntax in its macros, but most only take a small subset.

    In VB6 we got very little. Just the tiny conditional compilation subset of #Const and #If/#Else, but no named macros at all.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    444

    Re: Function vs Macro

    In simple English, macros are just text substitution, done on-the-fly before compilation begins. VB doesn't have much, but not often known is that you can define #Const globally by putting them in Project-->Properties-->Make tab-->Conditional Compilation. If you have more than one constant, you can separate them by ":". Example:

    DEBUG = 1 : DEMO = 1

    To disable them temporarily, use:

    DEBUG = 0 : DEMO = 0

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,646

    Re: Function vs Macro

    You can also supply values on the command line, but most people don't do a lot of batch compiling.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2018
    Posts
    180

    Re: Function vs Macro

    OK thanks guys.
    It's beginning to make sense to me.

  7. #7
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    648

    Re: Function vs Macro

    > "What is the difference between a Function and a Macro?"

    First important distinction: VB doesn't have macros. :-)

    In C++, macros are a sort of "inline functions" that are injected by the pre-processor. It basically re-writes your code in memory, substituting the "definition" of each Macro. This can be quite complex, because Macros can take arguments!

    Code:
    #define getmax(a,b) ((a)>(b)?(a):(b))
    
    int value = getmax( 37, strlen( (*aarvark)->name) );
    The pre-processor rewrites the source code on the fly, replacing it with this:

    Code:
    #define getmax(a,b) ((a)>(b)?(a):(b)) (Now just a comment)
    
    int value = ((37)>(strlen( (*aarvark)->name) ))?(37):(strlen( (*aarvark)->name) )));
    Now you being to understand why [good] Macro authors are a tad "liberal" with their brackets!

    If you really have to have something like this, the closest we have is Conditional Compilation.

    Regards, Phill W.

  8. #8
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    444

    Re: Function vs Macro

    Quote Originally Posted by Phill.W View Post
    First important distinction: VB doesn't have macros. :-)
    One could add macros to VB by making an Add-in. See this post for hidden events(BeginCompile/EnterDesignMode/EnterRunMode) that tells you when a compilation begins, and the post after about how to cancel the compilation. Once you make the substitutions, you can trigger the compilation by sending a message mentioned in that post to the VB IDE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width