Thread: returning multiple values from functions?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    2

    returning multiple values from functions?

    In C++, how can I have a function return multiple values?

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    There are two options. Either pass references/pointers as arguments (preferable) or create a struct that has all the variables you'd like and return it (not preferable). For example:
    Code:
    struct UglyStruct {
      int    some_thing;
      char   another_thing;
      string foo_bar;
    };
    
    UglyStruct some_func() {
      UglyStruct us;
      us.some_thing    = 1234;
      us.another_thing = '?';
      us.foo_bar       = "Blah";
      return us;
    }
    
    void some_func(int &int_ref, char &char_ref, string *ptr_string) {
      int_ref  = 2468;
      char_ref = '!';
      if (ptr_string != 0)
        *ptr_string = "*Cough cough*";
    }
    Last edited by LuckY; 08-23-2005 at 10:15 AM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    2
    Of course, pointers.

    My brain is melting.

    Thanks.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I wouldn't call the struct ugly. One might say that the reference or pointer parameters are confusing in that it is not immediately apparent that the argument will be modified by the function. I use both techniques and IMO both have their place. Neither is really preferable in general.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Of course it all depends entirely on context. There absolutely are cases where an individual will use a struct to group multiple variables together (I've done exactly that many a time). In fact, there are some cases where it's explicitly required (such as passing multiple params as an LPARAM in a Windows message or a void* in a child thread, for example), but I personally feel it is not a very attractive practice to create structs on-the-fly for simple situations where the struct will strictly be used for returning a pair of values. As an example, I would consider it "ugly" to do something like this:
    Code:
    struct RetData {
      string str;
      int    length;
    };
    
    RetData str_copy(const string &src) {
      RetData ret;
      ret.str    = src;
      ret.length = src.length();
      return ret;
    }
    when you could do this:
    Code:
    int str_copy(string &dest, const string &src) {
      dest = src;
      return dest.length();
    }
    or this:
    Code:
    void str_copy(string &dest, int &length, const string &src) {
      dest   = src;
      length = dest.length();
    }
    It's not that one is necessarily better than the other. Rather, in such a simple case, I feel that creating a struct strictly to return a pair of values (not to mention all the potential temporary variables that could be created) is overkill. But that's just me.
    Last edited by LuckY; 08-23-2005 at 01:06 PM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Boost's tuple has been added to TR1 and will be part of the next standard. Using that (or std::pair) would be preferable to your own handwritten structs if all you want to do is hold multiple values. For expensive to copy objects of course the pass-by-reference version saves the copy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple main functions
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 05-21-2008, 01:54 PM
  2. Returning Multiple Values
    By StarOrbs in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2005, 01:00 PM
  3. Functions returning char arrays
    By turmoil in forum C Programming
    Replies: 3
    Last Post: 05-27-2003, 01:43 AM
  4. Returning PID from execve family of functions?
    By ruairi in forum Linux Programming
    Replies: 2
    Last Post: 03-12-2003, 08:47 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM