Returning Multiple Values from a Function

This is a discussion on Returning Multiple Values from a Function within the C++ Programming forums, part of the General Programming Boards category; How can one return multiple values from a function ? Is there an example I can see ? CHECK OUT ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    Question Returning Multiple Values from a Function

    How can one return multiple values from a function ?

    Is there an example I can see ?

    CHECK OUT www.akilla.tk

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    The easiest way to return multiple variables is to define them in a structure and simply return that.

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct mystruct{
    	int x;
    	char y;
    	float z;
    }example, example2;
    
    mystruct myfunction(void);
    
    int main(){
    	example2 = myfunction();
    	cout<<example2.x<<" "<<example2.y<<" "<<example2.z;
    	cout.flush();
    	cin.get();
    	cin.get();
    	return(0);
    }
    mystruct myfunction(void){
    	cout<<"Enter a integer: ";
    	cin>>example.x;
    	cout<<"Enter a char: ";
    	cin>>example.y;
    	cout<<"Enter a float: ";
    	cin>>example.z;
    	return example;
    }
    Last edited by Traveller; 07-21-2002 at 10:29 AM.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> The easiest way to return multiple variables

    You can't return more than one variable, unfortunately.

    But, what you can and should is pass the variables that you want changed by reference.

    Eg.
    Code:
    #include <stdio.h>
    
    void function(int* num1,int* num2,int* num3)
    {
         *num1 = 10;
         *num2 = 20;
         *num3 = 30;
    }
    
    int main()
    {
         int one=1, two=2, three=3;
         function(one, two, three);
         printf("%d %d %d", one,two,three);
         return 0;
    }

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    317
    The easiest way to return multiple variables is to define them in a structure and simply return that.
    Try reading the whole response, before you correct, thanx.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> function(one, two, three);

    Sorry, that should've been

    function(&one,&two,&three);

  6. #6
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> Try reading the whole response, before you correct, thanx.

    huh?

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    The Dog, you were correcting the previous post but he was right. Struct can be returned, thus a multiple variable value. It's a naughty way of doing things though. I would go with the reference parameters as was suggested.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    5

    So...

    example2 = myfunction();

    so whatever variables written in myfunction will be stored in example2.... ?
    (in the case that both are of the same struct, i mean)

    can you also copy two structs directly ?

    example2 = example

    ??

    www.akilla.tk

  9. #9
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    I think someone misunderstood something.

    Check this:
    Code:
    typedef struct
    {
        /*blah blah blah*/
    }DOG;
    
    int main()
    {
         DOG busta;    //busta is a variable of type DOG
         return 0;
    }
    Can you return more than one DOG?
    Do you get what I'm saying?

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    The Dog,

    Respectfully, if I may,
    Code:
    #include <iostream>
    #include <conio.h>
    
    void function(int& num1,int& num2,int& num3)
    {
         num1 = 10;
         num2 = 20;
         num3 = 30;
    }
    
    int main()
    {
         int one = 1, two = 2, three = 3;
         std::cout << one << " " << two << " " << three << '\n' << std::endl;
         function(one, two, three);
         std::cout << one << " " << two << " " << three << '\n' << std::endl;
         std::cout << "Done!";
         getch();
         return 0;
    }
    No need to use the indirection operator when "passing by reference", however, thanks for letting me "play" with your code.

    P.S. Traveller, The Dog wasn't "correcting" your post that I could discern, just offering another option.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    Dog, Traveller was saying that you can put those extra values in a struct and return the struct. This "returns" more than one value within that struct. That is all. It's not major stuff here. He just didn't like that you corrected him when what he was saying was correct.

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    317
    Learner007:

    Yes, you can copy to structs directly, that is after all what the code is doing:

    Code:
    example2 = myfunction();
    
    /* Since my function returns a structure of type mystruct,
        example to be correct, the code executes myfunction first then
        the statement evaluates to:
    
         example2 = example;
    */
    Hope this answers your question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. c function returning multiple values
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-23-2001, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21