5 Replies - 1565 Views - Last Post: 30 June 2009 - 05:06 PM Rate Topic: -----

#1 ikofthetoids  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 18-December 08

function with multiple return values

Posted 30 June 2009 - 04:24 PM

I was wondering if it would be possible to have a function that would return multiple values. In my case, I have a function that I want to return the status of the mouse which includes the x/y coordinates and whether either button is clicked. I'd rather not have to define a struct or class for the return type, but just maybe have the function return an array (if possible). Any help?
Is This A Good Question/Topic? 0
  • +

Replies To: function with multiple return values

#2 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: function with multiple return values

Posted 30 June 2009 - 04:44 PM

you can return an array containing the two values just fine. A typical fix is to have the function take whatever arguments it needs, then pass it two more arguments of the variables you want to store the values in by address. The function will do its work, then assign the necessary values to those variables. :D
Was This Post Helpful? 0
  • +
  • -

#3 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: function with multiple return values

Posted 30 June 2009 - 04:51 PM

Quote

I was wondering if it would be possible to have a function that would return multiple values.
No, not possible.

Quote

I'd rather not have to define a struct or class for the return type,
Why not, excess work? True, this missing abstraction hurts sometimes. So you could end up jumping over to something like Boost Tuple but unless you are already using Boost, this might be excessive.

A struct it is. Besides, it documents the meaning of each variable (as opposed to arcane indices that need to be remembered.)

Quote

but just maybe have the function return an array (if possible).
There's a lot of issues with this kind of implementation. Let's say you solely wanted to return something. You can't really return arrays, but you can return pointers. What's the big deal here? Because you need something that exists outside the lifetime of a function, you dynamically allocate an array (yikes). And then return the pointer. Which the calling function must free (and you need to program this in). Kind of convoluted. The next best thing is to pass in an array, but I think a struct is still nicer.
Was This Post Helpful? 0
  • +
  • -

#4 oikonen  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 31
  • Joined: 03-December 07

Re: function with multiple return values

Posted 30 June 2009 - 04:52 PM

Return pointer to that array.

This post has been edited by oikonen: 30 June 2009 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: function with multiple return values

Posted 30 June 2009 - 05:02 PM

something called pass by reference

The variables passed in as x and y will have effectively been changed through the function.

void randomFunctionName( &x, &y);




Next best option is to create a struct (really isn't that bad)

This post has been edited by Notorion: 30 June 2009 - 05:02 PM

Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: function with multiple return values

Posted 30 June 2009 - 05:06 PM

well rather than reposting I will just like to my answer from last time I answered it: here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1