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. On 5/4/2011 12:27 PM, 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?


    There is more than one way to skin this cat. You can return
    'std::pair<int,int>', you can make those "out" arguments by passing them
    by reference (or by pointer)... I would recommend the former.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 4, 2011
    #2
    1. Advertising

  3. Huub

    Guest

    On May 4, 6:27 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.


    Hi Huub,

    The previous poster is correct. This example demonstrates the "call by
    reference" method to get what you want. This requires you to declare
    the integers before the function is called. The function is giving te
    addresses(or references) where the ints can be stored, the function
    does that.

    #include <stddef.h>
    #include <stdio.h>

    void GetTwoInts( int& a, int& b )
    {
    a = 3;
    b = 4;
    }

    int main()
    {
    int x,y;

    GetTwoInts( x, y );

    printf( "x from function GetTwoInts: %d\n", x );
    printf( "y from function GetTwoInts: %d\n", y );

    getchar();
    return 0;
    }

    Good luck with programming!
     
    , May 4, 2011
    #3
  4. Huub

    Kai-Uwe Bux 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 could return std::pair< int, int >. E.g.:

    typedef std::pair< int, int > int_pair;

    int_pair some_function ( some_arg_type some_arg ) {
    ...
    return ( int_pair( first_int, second_int ) );
    }


    Best,

    Kai-Uwe Bux
     
    Kai-Uwe Bux, May 4, 2011
    #4
  5. Huub

    crea Guest

    "Kai-Uwe Bux" <> wrote in message
    news:ips3ku$73f$...
    > 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 could return std::pair< int, int >. E.g.:
    >
    > typedef std::pair< int, int > int_pair;
    >
    > int_pair some_function ( some_arg_type some_arg ) {
    > ...
    > return ( int_pair( first_int, second_int ) );
    > }
    >


    wow, I have been programming like 15 years and never heard of this :).
    learning new...

    But are you guys using rather references or you normally use "pair" if you
    need to do this? Can pair s type be a reference so that it does not copy
    values?
     
    crea, May 5, 2011
    #5
  6. Huub

    Huub Guest

    On Wed, 04 May 2011 10:41:26 -0700, m_r_verhage wrote:

    > On May 4, 6:27 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.

    >
    > Hi Huub,
    >
    > The previous poster is correct. This example demonstrates the "call by
    > reference" method to get what you want. This requires you to declare the
    > integers before the function is called. The function is giving te
    > addresses(or references) where the ints can be stored, the function does
    > that.


    Thank you for the answer and example. I assume this also works when the
    function returning the value is in a different file, like a class?

    >
    > #include <stddef.h>
    > #include <stdio.h>
    >
    > void GetTwoInts( int& a, int& b )
    > {
    > a = 3;
    > b = 4;
    > }
    >
    > int main()
    > {
    > int x,y;
    >
    > GetTwoInts( x, y );
    >
    > printf( "x from function GetTwoInts: %d\n", x ); printf( "y from
    > function GetTwoInts: %d\n", y );
    >
    > getchar();
    > return 0;
    > }
    >
    > Good luck with programming!
     
    Huub, May 5, 2011
    #6
  7. Huub

    Nobody Guest

    On Thu, 05 May 2011 07:32:28 +0100, crea wrote:

    >> You could return std::pair< int, int >. E.g.:

    >
    > wow, I have been programming like 15 years and never heard of this :).


    I guess you haven't used std::map? Its iterator references a std::pair of
    the key and value.

    > But are you guys using rather references or you normally use "pair" if
    > you need to do this?


    It depends upon what's convenient. If the result was typically going to be
    stored in e.g. a std::vector<std::pair<int,int> > then I'd return a
    std::pair. Otherwise, I'd use reference parameters.

    > Can pair s type be a reference so that it does not
    > copy values?


    You can't return a reference to a locally-constructed object. The storage
    used for the object becomes invalid upon return.
     
    Nobody, May 5, 2011
    #7
  8. Huub

    Werner Guest

    On May 5, 8:32 am, "crea" <> wrote:
    > "Kai-Uwe Bux" <> wrote in message


    > >  typedef std::pair< int, int > int_pair;

    >
    > >  int_pair some_function ( some_arg_type some_arg ) {
    > >    ...
    > >    return ( int_pair( first_int, second_int ) );
    > >  }


    > But are you guys using rather references or you normally use "pair" if you
    > need to do this? Can pair s type be a reference so that it does not copy
    > values?


    It could, but would the original still exist after the function
    has exited? To what would the reference then refer (if it
    was initialized with something local (in scope) to the returning
    function)...?

    Crash boom bang...

    I haven't looked at the new standard that much, but perhaps
    using move semantics can solve this. I don't think that
    it would make any difference for builtin types though.

    //Perhaps transfers (or moves) the contents, the only
    // problem is that seeing that int has no "underlying"
    // data, there may be no gain.

    int_pair&& p = some_function();
     
    Werner, May 5, 2011
    #8
  9. Huub

    crea Guest

    "Nobody" <> wrote in message
    news:p...
    > On Thu, 05 May 2011 07:32:28 +0100, crea wrote:
    >
    >>> You could return std::pair< int, int >. E.g.:

    >>
    >> wow, I have been programming like 15 years and never heard of this :).

    >
    > I guess you haven't used std::map? Its iterator references a std::pair of
    > the key and value.


    I have used sometimes, but have not needed to use iterator. Just adding
    items and using them.
     
    crea, May 5, 2011
    #9
  10. Huub

    Huub Guest

    On Wed, 04 May 2011 10:41:26 -0700, m_r_verhage wrote:

    > On May 4, 6:27 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.

    >
    > Hi Huub,
    >
    > The previous poster is correct. This example demonstrates the "call by
    > reference" method to get what you want. This requires you to declare the
    > integers before the function is called. The function is giving te
    > addresses(or references) where the ints can be stored, the function does
    > that.
    >
    > #include <stddef.h>
    > #include <stdio.h>
    >
    > void GetTwoInts( int& a, int& b )
    > {
    > a = 3;
    > b = 4;
    > }
    >
    > int main()
    > {
    > int x,y;
    >
    > GetTwoInts( x, y );
    >
    > printf( "x from function GetTwoInts: %d\n", x ); printf( "y from
    > function GetTwoInts: %d\n", y );
    >
    > getchar();
    > return 0;
    > }
    >
    > Good luck with programming!


    Thank you. This works.
     
    Huub, May 5, 2011
    #10
  11. Huub

    Nobody Guest

    On Thu, 05 May 2011 00:39:50 -0700, Werner wrote:

    > I haven't looked at the new standard that much, but perhaps
    > using move semantics can solve this. I don't think that
    > it would make any difference for builtin types though.


    AIUI, move semantics are meant for the situation where the type contains
    pointers or references, allowing the copy (i.e. move) to be a shallow
    copy, where "ownership" of the referenced object is transferred from the
    source to the destination.

    I would expect the copy to be elided in the case where the callee is
    an inline function.

    On some platforms, the C calling convention implements the return of
    "large" objects by having the caller provide a pointer to the storage as
    an additional argument, but I don't know if this is commonly done for C++
    (or even if it's possible, due to issues with user-defined copy
    constructors or assignment operators).
     
    Nobody, May 6, 2011
    #11
  12. Huub

    none Guest

    In article <qwrwp.61790$2>, crea <> wrote:
    >
    >"Kai-Uwe Bux" <> wrote in message
    >news:ips3ku$73f$...
    >> 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 could return std::pair< int, int >. E.g.:
    >>
    >> typedef std::pair< int, int > int_pair;
    >>
    >> int_pair some_function ( some_arg_type some_arg ) {
    >> ...
    >> return ( int_pair( first_int, second_int ) );
    >> }
    >>

    >
    >wow, I have been programming like 15 years and never heard of this :).
    >learning new...
    >
    >But are you guys using rather references or you normally use "pair" if you
    >need to do this? Can pair s type be a reference so that it does not copy
    >values?


    This raises at least three questions:

    1- Why are you worrying that the pair get copied?
    (hint: premature optimisation)

    2- Why do you think that a copy actually happens rather than being
    optimised away by the compiler?

    3- Why do you prefer using what is an unnatural and more verbose
    syntax e.g.:

    ---------------------------------
    // Header
    void some_function(some_arg_type some_arg, int &a, int & b);

    // Usage
    int a = 0; // Watch out if you don't pre-initialise
    int b = 0;
    some_function(some_arg, a, b);
    ----------------------------------
    Over what is a more natural, less verbose and probably less error prone.

    ----------------------------------
    // Header
    typedef std::pair< int, int > int_pair;
    int_pair some_function ( some_arg_type some_arg );

    // Usage
    int_pair answer = some_function(some_arg);
    -----------------------------------

    Note that while it may seem that the 2 lines in the header are worse
    than the 1 line for the first case, you need three line everytime you
    use the function which makes it much worse overall.

    Note that the second form scales nicely to 3, 4, 5, 100 results by
    using a struct as a return type or a container. The first form will
    and up worse and worse.

    Also, as hinted, you really should pre-initialise the response data
    holder with your proposed technique. Failure to do so might hide a
    very subtle bug and even then, you are relying on magic values. But
    the net effect is that you code will end up writing data to a and b
    twice which depending on the type of a and b might make the first
    method actually slower than the second.

    Yannick
     
    none, May 6, 2011
    #12
  13. Huub

    Krice Guest

    On 4 touko, 19:27, Huub <v.niekerk_@_hccnet.nl> 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?


    One of the good programming practices is keep return values simple.
    If you need to return two random ints you are most likely doing
    something wrong.
    If you need to return values like x, y (location) then the proper
    way is create an user defined type/class and return it.
     
    Krice, May 7, 2011
    #13
  14. Huub

    none Guest

    In article <>,
    Krice <> wrote:
    >On 4 touko, 19:27, Huub <v.niekerk_@_hccnet.nl> 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?

    >
    >One of the good programming practices is keep return values simple.


    Hmm... not in the generic sense of 'simple'.

    >If you need to return two random ints you are most likely doing
    >something wrong.


    OK, as long as what you call complex return value are multiple
    unrelated values, your first statement is ok.

    >If you need to return values like x, y (location) then the proper
    >way is create an user defined type/class and return it.


    Exactly, then this return value can be as complex as needed (and as
    simple as possible) as long as it is coherent.
     
    none, May 9, 2011
    #14
    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:
    498
    Richard Bos
    Jun 18, 2004
  2. Greenhorn
    Replies:
    15
    Views:
    840
    Keith Thompson
    Mar 6, 2005
  3. Replies:
    4
    Views:
    552
    James Kanze
    Jul 22, 2008
  4. AlexHWGUY
    Replies:
    5
    Views:
    162
    John W. Krahn
    Oct 24, 2006
  5. strout

    Function return function???

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

Share This Page