Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ return value from function

C++ return value from function

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++testingbeta-testing
9 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uusheikh
    wrote on last edited by
    #1

    Hi there, i was testing this the other day, and it compiles fine. int Test(int a, int b, int c) { return a,b,c; } doing this; printf("\n %d", Test(10,20,30)); will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)

    N D L 3 Replies Last reply
    0
    • U uusheikh

      Hi there, i was testing this the other day, and it compiles fine. int Test(int a, int b, int c) { return a,b,c; } doing this; printf("\n %d", Test(10,20,30)); will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      The way the comma operator works is that it returns the last value. For instance:

      int a = 1, 2, 3;

      will result in a being 3. So your

      return a,b,c;

      is really equivalent to:

      return c;

      utf8-cpp

      U 1 Reply Last reply
      0
      • N Nemanja Trifunovic

        The way the comma operator works is that it returns the last value. For instance:

        int a = 1, 2, 3;

        will result in a being 3. So your

        return a,b,c;

        is really equivalent to:

        return c;

        utf8-cpp

        U Offline
        U Offline
        uusheikh
        wrote on last edited by
        #3

        If so, what is the use of the comma operator? Is there real use for it?

        M N U 3 Replies Last reply
        0
        • U uusheikh

          If so, what is the use of the comma operator? Is there real use for it?

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          for example it's used in the for statement.

          for ( i = 0, j = 0;; i<100; i++, j++)
          {
          // ...
          }

          or in variable declarations:

          int i, j;

          and maybe many other places.

          This signature was proudly tested on animals.

          1 Reply Last reply
          0
          • U uusheikh

            If so, what is the use of the comma operator? Is there real use for it?

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            It can be overloaded. See Boost Assign[^] library for example:

            vector<int> v;
            v += 1,2,3,4,5,6,7,8,9;

            utf8-cpp

            1 Reply Last reply
            0
            • U uusheikh

              If so, what is the use of the comma operator? Is there real use for it?

              U Offline
              U Offline
              uusheikh
              wrote on last edited by
              #6

              Oh yes, comma is used in for loops. It's so common! I didn't realize it! Thanks!

              P 1 Reply Last reply
              0
              • U uusheikh

                Hi there, i was testing this the other day, and it compiles fine. int Test(int a, int b, int c) { return a,b,c; } doing this; printf("\n %d", Test(10,20,30)); will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                uus831 wrote:

                Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies?

                See here and here.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                1 Reply Last reply
                0
                • U uusheikh

                  Oh yes, comma is used in for loops. It's so common! I didn't realize it! Thanks!

                  P Offline
                  P Offline
                  peterchen
                  wrote on last edited by
                  #8

                  The comma operator evaluates bot operands, and evaluates to the result of the second one. So the first operand is evaluated only for its side effects. One common use is: while ( (c=getnextthingie(), c!=EOF) ) { } here, the first operand has the side effect of calling getnextthingie and assigning the result to c. c!=EOF is evaluated as loop condition.

                  Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
                  | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server

                  1 Reply Last reply
                  0
                  • U uusheikh

                    Hi there, i was testing this the other day, and it compiles fine. int Test(int a, int b, int c) { return a,b,c; } doing this; printf("\n %d", Test(10,20,30)); will always give 30. Can anyone explain what is actually happening in the code above, and why it is even possible to compile such a code? what does return a,b,c actually implies? I never seen any code that does that, (all codes return 1 value), but if this code can compile, what does it mean? (Tested using VC++2008)

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Er, yeah, if you COULD declare your function as: int, int, int Test(int a, int b, int c); You might get 12 bytes on the stack for your return values, but C isnt made this way! If you realy want to wedge more values into the 32 bits you are given you could cast say a short and two chars into an int. return (x << 16 + y << 8 + z); for example, and then pull it apart in the calling code.

                    Morality is indistinguishable from social proscription

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups