return from function

Discussion in 'C++' started by Huub, May 4, 2011.

  1. Huub

    Huub Guest

    Hi,

    I made a function that should return 2 integers. But as the compiler
    indicates in "return x,y", the x is ignored. So, it is even
    possible what I want: returning 2 integers?

    Thanks.
     
    Huub, May 4, 2011
    #1
    1. Advertising

  2. Huub

    Marc Guest

    Huub wrote:

    > I made a function that should return 2 integers. But as the compiler
    > indicates in "return x,y", the x is ignored. So, it is even
    > possible what I want: returning 2 integers?


    You can only return one thing from a function. But nothing prevents
    that thing from being a std::pair of integers. The other usual
    solution is to pass integers as arguments by reference to the
    function.
     
    Marc, May 4, 2011
    #2
    1. Advertising

  3. On May 4, 1:31 pm, Huub <v.niekerk_@_hccnet.nl> wrote:
    > Hi,
    >
    > I made a function that should return 2 integers. But as the compiler
    > indicates in "return x,y", the x is ignored. So, it is even
    > possible what I want: returning 2 integers?
    >
    > Thanks.


    "return x,y;" is parsed as a return statement with an expression
    "x,y". x and y are names of integer variables. The expression "x,y" is
    two expressions "joined" by the comma operator. The comma operator
    evaluates the first argument, has a sequence point, then the second
    argument. The result of a comma operator is the evaluation of the
    second expression.

    Note that commas in function calls are not comma operators, and commas
    in other contexts are not necessarily comma operators either.
    Consider:
    f(x, y); //not a comma operator
    f((x, y)); //is a comma operator
    x, y; //is a comma operator
    return x, y; //is a comma operator
    int x, y; //not a comma operator
     
    Joshua Maurice, May 5, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Seong-Kook Shin
    Replies:
    1
    Views:
    511
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    868
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    4
    Views:
    561
    James Kanze
    Jul 22, 2008
  4. AlexHWGUY
    Replies:
    5
    Views:
    173
    John W. Krahn
    Oct 24, 2006
  5. strout

    Function return function???

    strout, Feb 24, 2005, in forum: Javascript
    Replies:
    17
    Views:
    160
    Jarmo
    Feb 27, 2005
Loading...

Share This Page