Thread: Having a function return three values

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    24

    Having a function return three values

    In one cpp file I have three particular variables and a loop. From within this loop I want to pass the three variables to a function in another cpp file. Before I do this I want to pass them to an intermediate function in another cpp file for some processing.

    If that intermediate function could return three values then I'd have no problem. I've considered calling the third function from the second but I'd rather keep all the calls from within the original loop to save any future confusion. I've thought of using pointers to variables but that seems untidy, and I can only think of using global variables otherwise.

    Is there a good way of doing this or even a creative workaround?

  2. #2
    Registered User
    Join Date
    Jul 2009
    Posts
    50
    Aside from passing by reference, you could create a structure which holds all your variables and return that.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Or, since you are using C++, you could create a class, pass the instance of the class, and use getters and setters to access the variables.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    Not sure but would a class seem overkill for three floats? Would the point be to fill the class with some of the surrounding code?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Some languages (python) offer something called a tuple which seems to be just what you want. C++ doesn't offer this as it's very type conscious, though it can be achieved using the boost library

    The Boost Tuple Library

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    pointers/references are not untidy, that is the sensible thing to do.

  7. #7
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    In this case, use 3 references, or 1 reference to a struct/class wrapping the 3 variables.

    In this case, some people would put an empty #define ref, and use call(ref someVar) so it appears more "tidy."
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    Quote Originally Posted by alanb View Post
    Not sure but would a class seem overkill for three floats? Would the point be to fill the class with some of the surrounding code?
    What exactly does your program do? Judging by your description in your original post, it sounds like these 3 floats are 3 variables that describe an object; in which it would be a good idea to use a class.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    24
    Thanks for the by reference suggestions and the info on pointers, this will help me in future.

    I think I'll try a class. The more I think of it, the more it seems appropriate for later expansion.

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    you'll still want to pass that class by an indirect type to avoid superfluous temporaries.

  11. #11
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by Dae View Post
    In this case, some people would put an empty #define ref, and use call(ref someVar) so it appears more "tidy."
    Sucks for someone who writes int ref = 0; though...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Tuples are in TR1 too, by the way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Magos View Post
    Sucks for someone who writes int ref = 0; though...
    Or anyone who redefines ref, luckily it's just an example name, and compilers are smart enough to throw an error if a user should use their own code or library incorrectly.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM