CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2012
    Location
    Australia
    Posts
    15

    Difference between function and macro

    Hello expects. Can you tell me wot is the difference between a function and macro in C++. They lok the sam. Can you tell? When you use a macro and not a function?
    Last edited by Steve R Jones; October 1st, 2015 at 05:38 AM.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Difference between function and macro

    A macro replaces the original text in the code before the compiler start to process the file. I.e. every single place where you use the macro will get a duplicate of the code.

    A function is a complete section of code i.e. no duplication occur.

    Consider macros to be evil and stick to using functions instead, at least until you have a LOT of programming experience. There is no end in how many hard to solve issues you can create for yourself with the use of macros...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Difference between function and macro

    Quote Originally Posted by Yarlini Amirthanesan View Post
    Hello expects. Can you tell me wot is the difference between a function and macro in C++. They lok the sam. Can you tell? When you use a macro and not a function?

    Email: yarlini.amirthanesan@dsto.defence.gov.au
    No: From the call alone, it is not possible to know if you are using a macro, a function, or an object's operator().

    However, there is a very widespread coding practice that says that macros should be in ALL_CAPS:
    Code:
    DO_SOMETHING(); //Probably a macro
    do_something(); //Probably a function
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    May 2012
    Location
    Australia
    Posts
    15

    Re: Difference between function and macro

    wow. good reply.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Difference between function and macro

    In 99% of cases, inline functions are superior to macros.

    The one thing which macros can do that functions can't, is to take complex statements as arguments. Usually, this just obscures things, but from time to time it can be useful.

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Difference between function and macro

    Another downside to macros is that they don't obey the C++ scoping rules.
    ANYTHING that matches the macro name is replaced by the pre-processor.

    Anybody who has named a member function GetMessage when using Windows and MFC will know what I mean.

    (The preprocessor will replace ANY GetMessage with GetMessageA or GetMessageW, depending on whether the setup is ASCII or Unicode. The compiler will then complain that your class doesn't contain a definition of one of these! )
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured