How can one return multiple values from a function ?
Is there an example I can see ?
CHECK OUT www.akilla.tk
How can one return multiple values from a function ?
Is there an example I can see ?
CHECK OUT www.akilla.tk
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 09:29 AM.
>> 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; }
Try reading the whole response, before you correct, thanx.The easiest way to return multiple variables is to define them in a structure and simply return that.
>> function(one, two, three);
Sorry, that should've been
function(&one,&two,&three);
>> Try reading the whole response, before you correct, thanx.
huh?
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.
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
I think someone misunderstood something.
Check this:
Can you return more than one DOG?Code:typedef struct { /*blah blah blah*/ }DOG; int main() { DOG busta; //busta is a variable of type DOG return 0; }
Do you get what I'm saying?
The Dog,
Respectfully, if I may,
No need to use the indirection operator when "passing by reference", however, thanks for letting me "play" with your code.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; }
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
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.
Learner007:
Yes, you can copy to structs directly, that is after all what the code is doing:
Hope this answers your question.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; */