Jump to content
  • Advertisement

Archived

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

MrPoopypants

Quick Question -- Howdo i get a function to return multiple values

This topic is 5792 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

(C++)
    
int GenerateRandomOblio ( int a , int b , int c , int d )
{
	srand ( time( 0 ) );
	
	do
	{
	a = rand() % 9 + 1;
	b = rand() % 9 + 1;
	c = rand() % 9 + 1;
	d = rand() % 9 + 1;
	}
	while( a != b && a != c && a != d && b != c && b != d && c != d );



	return (a , b , c , d);
}
    
When i run this program and cout the value, it displays only a. (What i am trying to do is return four integers != to one another) Any help would be appreciated. thanks [edited by - MrPoopypants on December 6, 2002 8:20:03 PM]

Share this post


Link to post
Share on other sites
Advertisement
You can only return one value from a function.

However, you can return a pointer to something. IE -- a pointer a to a structure.

Also, you can pass those integers as pointers to the function and then you can modify those so that when you return from the function, a, b, c, and d are modified to reflect what you did with them in the function (I think this is what you want to do).

[edited by - randomDecay on December 6, 2002 8:21:37 PM]

Share this post


Link to post
Share on other sites

  
struct ret_value
{
int a,b,c,d;
};
ret_value myfunc()
{
...
}

or
pair<pair<int,int>,pair<int,int> > myfunc()
{
return make_pair(make_pair(a,b),make_pair(c,d));
}

or even better, check out boost::tuple

edit: code vs source tags

[edited by - sjelkjd on December 6, 2002 8:34:15 PM]

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!