Thread: Return 2 variables?

  1. #1
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69

    Return 2 variables?

    Is it possible to return 2 variables for ex.
    Code:
    int hello(int x, int y)
    {
       x++;
       y++;
       return x,y;
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    void hello(int &x, int &y) {
      x++;
      y++;
    }

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    No. But you can wrap them up in a struct and then return the struct.

    Code:
    struct Return2Ints {
      int x;
      int y;
    };
    
    Return2Ints hello (int x, int y)
    {
      return Return2Ints(x+1,y+1);
    }

  4. #4
    Hi ay_okay's Avatar
    Join Date
    Dec 2004
    Location
    Here
    Posts
    69
    how would i recieve it?
    Code:
    x = return2numbers();
    xbeing a struct?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    From Thantos' example:
    Code:
    Return2Ints someStruct = hello (1,2);

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Adding to bithub:
    To get to the actual values
    Code:
    int a = someStruct.x;
    int b = someStruct.y;

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    A generic way to do this is to return an std::pair:

    std::pair<int,int> hello(int x, int y); { return std::make_pair (x,y); }

    std::pair bla = hello(1,2);
    cout << bla.first; //x

    Tuples (look up Boost.Tuple) let you return any number of values. You can use Boost.Tuple in a similar way to std::pair.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> return Return2Ints(x+1,y+1);
    Return2Ints needs a proper constructor for that to work.
    Code:
    struct Return2Ints {
      int x;
      int y;
    
      Return2Ints(int x_, int y_) : x(x_), y(y_) {}
    };
    gg

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Opps forgot about that. Though it can be done without a constructor:
    Code:
    Return2Ints hello(int x, int y)
    {
      Return2Ints ret = {x+1,y+1};
      return ret;
    }

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by azteched
    A generic way to do this is to return an std::pair:

    std::pair<int,int> hello(int x, int y); { return std::make_pair (x,y); }

    std::pair<int,int> bla = hello(1,2);
    cout << bla.first; //x

    Tuples (look up Boost.Tuple) let you return any number of values. You can use Boost.Tuple in a similar way to std::pair.
    Added part in red above.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  2. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  3. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM