Difference between a MACRO and a FUNCTION

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MAx

    #1

    Difference between a MACRO and a FUNCTION

    Hi,
    Could any one list possible number of diferences between a function
    and a macro?
    which one will be faster and why??

    ex : function and a macro ti find max of two nums
    #define MAX(a,b) (a>b) ? a:b

    and
    int max(int a,int b)

  • Ian Collins

    #2
    Re: Difference between a MACRO and a FUNCTION

    MAx wrote:
    Hi,
    Could any one list possible number of diferences between a function
    and a macro?
    Homework?
    which one will be faster and why??
    >
    The answer is either or neither.

    --
    Ian Collins.

    Comment

    • Mark Bluemel

      #3
      Re: Difference between a MACRO and a FUNCTION

      MAx wrote:
      Hi,
      Could any one list possible number of diferences between a function
      and a macro?
      which one will be faster and why??
      Do I smell homework?

      Comment

      • MAx

        #4
        Re: Difference between a MACRO and a FUNCTION

        On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
        MAx wrote:
        Hi,
        Could any one list possible number of diferences between a function
        and a macro?
        which one will be faster and why??
        >
        Do I smell homework?
        obviously not a homework.
        this was an interview question...
        i am looking for an impressive answer.

        Comment

        • Mark Bluemel

          #5
          Re: Difference between a MACRO and a FUNCTION

          MAx wrote:
          On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
          >MAx wrote:
          >>Hi,
          >>Could any one list possible number of diferences between a function
          >>and a macro?
          >>which one will be faster and why??
          >Do I smell homework?
          >
          obviously not a homework.
          There's no "obviously" about it...
          this was an interview question...
          i am looking for an impressive answer.
          Then I suggest you do some research - other than asking in a newsgroup -
          and produce one.

          Comment

          • MAx

            #6
            Re: Difference between a MACRO and a FUNCTION

            On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
            MAx wrote:
            On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
            MAx wrote:
            >Hi,
            >Could any one list possible number of diferences between a function
            >and a macro?
            >which one will be faster and why??
            Do I smell homework?
            >
            obviously not a homework.
            >
            There's no "obviously" about it...
            >
            this was an interview question...
            i am looking for an impressive answer.
            >
            Then I suggest you do some research - other than asking in a newsgroup -
            and produce one.
            Thanks for the help

            Comment

            • Army1987

              #7
              Re: Difference between a MACRO and a FUNCTION

              On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
              Hi,
              Could any one list possible number of diferences between a function
              and a macro?
              which one will be faster and why??
              The macro will usually be faster, unless the compiler is smart
              enough to inline the function call. Since C99 you can suggest the
              compiler to do so by using the 'inline' keyword.
              ex : function and a macro ti find max of two nums
              #define MAX(a,b) (a>b) ? a:b
              Please add enough parentheses:
              ((a) (b) ? a : (b))
              As you wrote it, using complicated expressions could yield the
              wrong semantic due to the precedence (actually, syntax) rules.

              Note that the one returned will have been evaluated twice, so be
              careful not to pass it expression with side effects.
              and
              int max(int a,int b)
              This one doesn't suffer the problems described above, but it will
              be much slower than the macro unless the compiler inlines it, and
              it always convert the arguments to int and return an int, where
              the macro return the "correct" type depending on the type of the
              arguments.

              --
              Army1987 (Replace "NOSPAM" with "email")
              If you're sending e-mail from a Windows machine, turn off Microsoft's
              stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
              characters through your mail. -- Eric S. Raymond and Rick Moen

              Comment

              • Richard Heathfield

                #8
                Re: Difference between a MACRO and a FUNCTION

                Army1987 said:
                On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
                <snip>
                >#define MAX(a,b) (a>b) ? a:b
                >
                Please add enough parentheses:
                ((a) (b) ? a : (b))
                You missed some.

                <snip>

                --
                Richard Heathfield <http://www.cpax.org.uk >
                Email: -www. +rjh@
                Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                "Usenet is a strange place" - dmr 29 July 1999

                Comment

                • Philip Potter

                  #9
                  Re: Difference between a MACRO and a FUNCTION

                  Richard Heathfield wrote:
                  Army1987 said:
                  >On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
                  >
                  <snip>
                  >
                  >>#define MAX(a,b) (a>b) ? a:b
                  >Please add enough parentheses:
                  >((a) (b) ? a : (b))
                  >
                  You missed some.
                  >
                  <snip>
                  Really?

                  (((a)>(b)) ? a : (b))

                  has the same meaning, as does

                  ((a)>(b) ? (a) : (b))

                  Though this does look nicer due to consistency.

                  The only other places I can think to add parentheses are either redundant ((a))
                  or obviously wrong ((a) ((b) ? a : (b)))

                  So what did he miss?

                  Phil

                  --
                  Philip Potter pgp <atdoc.ic.ac. uk

                  Comment

                  • Mark Bluemel

                    #10
                    Re: Difference between a MACRO and a FUNCTION

                    MAx wrote:
                    On Sep 20, 1:51 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
                    >MAx wrote:
                    >>On Sep 20, 1:10 pm, Mark Bluemel <mark_blue...@p obox.comwrote:
                    >>>MAx wrote:
                    >>>>Hi,
                    >>>>Could any one list possible number of diferences between a function
                    >>>>and a macro?
                    >>>>which one will be faster and why??
                    >>>Do I smell homework?
                    >>obviously not a homework.
                    >There's no "obviously" about it...
                    >>
                    >>this was an interview question...
                    >>i am looking for an impressive answer.
                    >Then I suggest you do some research - other than asking in a newsgroup -
                    >and produce one.
                    >
                    Thanks for the help
                    >
                    You're welcome. I wasn't just being snitty (though that's probably part
                    of it).

                    This actually does, in effect, come under the "homework" heading. The
                    point of the exercise should be for you to learn something for yourself
                    by putting in the necessary effort, rather than being spoonfed something
                    which you can hand in to your tutor or spout to your interviewer.

                    Comment

                    • Army1987

                      #11
                      Re: Difference between a MACRO and a FUNCTION

                      On Thu, 20 Sep 2007 11:04:55 +0000, Richard Heathfield wrote:
                      Army1987 said:
                      >On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
                      >
                      <snip>
                      >
                      >>#define MAX(a,b) (a>b) ? a:b
                      >>
                      >Please add enough parentheses:
                      >((a) (b) ? a : (b))
                      >
                      You missed some.
                      Syntax
                      1 conditional-expression:
                      logical-OR-expression
                      logical-OR-expression ? expression : conditional-expression

                      (As someone pointed out on this newsgroup, ? and : can only occur
                      in pairs, so they are like [ and ], or ( and ) for this matter.)
                      --
                      Army1987 (Replace "NOSPAM" with "email")
                      If you're sending e-mail from a Windows machine, turn off Microsoft's
                      stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                      characters through your mail. -- Eric S. Raymond and Rick Moen

                      Comment

                      • Richard Heathfield

                        #12
                        Re: Difference between a MACRO and a FUNCTION

                        Philip Potter said:
                        Richard Heathfield wrote:
                        >Army1987 said:
                        >>On Thu, 20 Sep 2007 00:04:05 -0700, MAx wrote:
                        >>
                        ><snip>
                        >>
                        >>>#define MAX(a,b) (a>b) ? a:b
                        >>Please add enough parentheses:
                        >>((a) (b) ? a : (b))
                        >>
                        >You missed some.
                        >>
                        ><snip>
                        >
                        Really?
                        No, not really - at least, I just tried three times to break his version
                        and failed each time. So I owe him an apology.
                        (((a)>(b)) ? a : (b))
                        >
                        has the same meaning, as does
                        >
                        ((a)>(b) ? (a) : (b))
                        >
                        Though this does look nicer due to consistency.
                        Yes, it does, doesn't it? Emerson notwithstanding ...

                        --
                        Richard Heathfield <http://www.cpax.org.uk >
                        Email: -www. +rjh@
                        Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                        "Usenet is a strange place" - dmr 29 July 1999

                        Comment

                        • Army1987

                          #13
                          Re: Difference between a MACRO and a FUNCTION

                          On Thu, 20 Sep 2007 11:44:58 +0000, Richard Heathfield wrote:
                          Philip Potter said:
                          >Richard Heathfield wrote:
                          >>Army1987 said:
                          >>>Please add enough parentheses:
                          >>>((a) (b) ? a : (b))
                          >>You missed some.
                          [snip]
                          >(((a)>(b)) ? a : (b))
                          >>
                          >has the same meaning, as does
                          >>
                          >((a)>(b) ? (a) : (b))
                          >>
                          >Though this does look nicer due to consistency.
                          >
                          Yes, it does, doesn't it? Emerson notwithstanding ...
                          It does (maybe whitespace around will add even more
                          consistency?), but who's Emerson?
                          --
                          Army1987 (Replace "NOSPAM" with "email")
                          If you're sending e-mail from a Windows machine, turn off Microsoft's
                          stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
                          characters through your mail. -- Eric S. Raymond and Rick Moen

                          Comment

                          • Eberhard Schefold

                            #14
                            Re: Difference between a MACRO and a FUNCTION

                            Army1987 wrote:
                            It does (maybe whitespace around will add even more
                            consistency?), but who's Emerson?
                            "A foolish consistency is the hobgoblin of little minds."
                            -- Ralph Waldo Emerson (1803–1882)

                            Don't think I'm a literary person, but I remembered the line from
                            "What's Up, Doc". :-)

                            Comment

                            • John Bode

                              #15
                              Re: Difference between a MACRO and a FUNCTION

                              On Sep 20, 2:04 am, MAx <mahesh1...@gma il.comwrote:
                              Hi,
                              Could any one list possible number of diferences between a function
                              and a macro?
                              Grab your handy C reference and read the section about the
                              preprocessor, focusing on the terms "definition " and "replacemen t".
                              After that, it should be quite obvious what the differences between a
                              function and a function-like macro are.
                              which one will be faster and why??
                              That depends on the operations involved. For your example below, all
                              things being equal, the macro should be faster. Again, once you've
                              read up on what exactly the preprocessor does with a function-like
                              macro definition, the reason why should be fairly evident.
                              >
                              ex : function and a macro ti find max of two nums
                              #define MAX(a,b) (a>b) ? a:b
                              >
                              and
                              int max(int a,int b)


                              Comment

                              Working...