HighDots Forums  

passing multiple values from function?

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss passing multiple values from function? in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
FunGuySF
 
Posts: n/a

Default passing multiple values from function? - 01-03-2005 , 11:43 AM






I have created a function, from which I need to return multiple values.

function multi(value) {
... processing ...;
return value1, value2, value3;
}

My question is, when I call the function, how can I access those multiple
returned values?

is it something like:
value1, value2, value3 = multi(value);

or maybe:
result = multi(value);
value1 = result.value1;
value2 = result.value2;
value3 = result.value3;

Where are those returned values?

Thanks!

Mike




Reply With Quote
  #2  
Old   
Guido Wesdorp
 
Posts: n/a

Default Re: passing multiple values from function? - 01-05-2005 , 10:53 AM






FunGuySF wrote:
Quote:
I have created a function, from which I need to return multiple values.

function multi(value) {
... processing ...;
return value1, value2, value3;
}

My question is, when I call the function, how can I access those multiple
returned values?

is it something like:
value1, value2, value3 = multi(value);

Unfortunately it's not Python we're dealing with, so this won't work.
What you could do is something like:

function multi(value) {
... processing ...;
return [value1, value2, value3];
};

var result = multi(value);
var value1 = result[0];
var value2 = result[1];
var value3 = result[2];

I know this is a bit verbose, but I reckon it's the easiest solution.
Another one would be to generate an object with the return values:

function multi(value) {
... processing ...;
return {'value1': value1,
'value2': value2,
'value3': value3};
};

var result = multi(value);
// now you can use result.value1, result.value2, result.value3

Hope that helps.

Cheers,

Guido


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.