CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2002
    Location
    Chandigarh, India
    Posts
    91

    get more than one value from a function

    Is there a way to get more than one value from a function. Presently function just return one value as specified in its return type.

  2. #2
    Join Date
    Sep 2001
    Location
    San Diego
    Posts
    2,147
    You can pass in pointers or references to functions. These can be used to return information to the caller.

    For example:

    Code:
    // some routine
    {
             
             int FirstParam = 0;
             int SecondParam = 0;
    
             MultipleReturns(FirstParam, &SecondParam);
    
             printf("FirstParam = %d, SecondParam = %d\n", FirstParam, SecondParam);
    
    }
    
    
    // The function returning more than one thing
    void MultipleReturns(int & First, int *Second)
    {
    
             First = 21;
             *Second = 45;
    
    }
    You'll notice that FirstParam is passed by reference, and SecondParam is passed as a pointer.

    Just for grins, I declared the routine void (no return value).

    Hope this helps,

    - Nigel

  3. #3
    Join Date
    Jul 2002
    Location
    Chandigarh, India
    Posts
    91

    achieved retrieving values

    Thaknks for your advise. I am able to get the values as I wanted it to be.

  4. #4
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773
    You can also return 2 values by using std::pair< T1, T2 > where T1 and T2 are the types.

    Any more and you can always put them into a struct and return the struct.

    The other option, as been mentioned above, is for the function to take non-const references (or pointers) and write the values into there.

  5. #5
    Join Date
    Jul 2004
    Posts
    98
    If there are a lot of variables let's say int

    int MyArray[10];

    you can send int* MyArray as an argument to the function and then access the whole array by reference.

    Ted

  6. #6
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Another option is to try boost::tuple.

  7. #7
    Join Date
    Apr 2004
    Location
    In the back seat of New Horizons.
    Posts
    1,238
    Quote Originally Posted by KevinHall
    Another option is to try boost::tuple.
    What do you mean exactly? I've never heard of this.
    Here are the rules, you must obey them or the gender bender will get you.

    And if you ever think of posting without code-tags, the evil monkey will come after you.

  8. #8
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Boost is a library of features that people hope will be added to the C++ standard. Actually, there's more to it than that, but I digress. So you can view boost as a free addon library.

    Anyway, here is the documentation for boost::tuple.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured