Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

paulkp

returning variables

This topic is 6226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to return two variables from a function. What is the most simple way to do this with C++--?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
you cant return 2 variables, but you can pass the address of 2 variables and change them in the function

ex.
void func(int *v1, int *v2)
{
*v1 = 1;
*v2 = 2;
}

to call
int var1, var2;
func(&var1, &var2);

now var1 will = 1, and var2 will = 2

Share this post


Link to post
Share on other sites
Similarly to the previous post:

void func(int &v1, int &v2)
{
v1 = 1;
v2 = 2;
}

and to call it:

int var1, var2;

func(var1, var2);

var1 will equal 1 and var2 will be 2.

Edited by - Akhenaten on June 29, 2001 10:41:43 AM

Share this post


Link to post
Share on other sites
The C++ standard library provides a ''pair'' template:

  
pair<int, double> return_two_vars(int x)
{
return make_pair(10, 10.5);
}


(Or something like that.)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!