473,513 Members | 2,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

returning a list

I'd like to return two values from a function. I'm doing this:

function getTwo(){
// some code
return new Array(value_a,value_b);
}

var returnArray=getTwo(some_var);

var value_a=returnArray[0];
var value_b=returnArray[1];

Is there a cleaner way of doing that? I'm used to perl list assignments.

Cheers,
Jeff
Jul 20 '05 #1
4 1724
JRS: In article <0s*****************@newsread1.news.atl.earthlink. net>,
seen in news:comp.lang.javascript, Jeff Thies <no****@nospam.net> posted
at Thu, 18 Dec 2003 15:10:52 :-
I'd like to return two values from a function. I'm doing this:

function getTwo(){
// some code
return new Array(value_a,value_b);
}

var returnArray=getTwo(some_var);

var value_a=returnArray[0];
var value_b=returnArray[1];

Is there a cleaner way of doing that? I'm used to perl list assignments.


Yes. You could have written return [value_a, value_b] } .

However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

then with (getTwo(some_var)) { var value_a = A, value_b = B }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics, links.
Jul 20 '05 #2
> However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .
That's really nice! I'm calling a function that calculates many different
properties. Now I can retrieve any of these that are in the hash. Perfect
for astronomical calculations.

BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
yesterday!

Cheers,
Jeff
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 © <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript <URL:http://www.merlyn.demon.co.uk/js-index.htm> Jsc maths, dates, sources. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/Jsc/&c, FAQ topics,

links.
Jul 20 '05 #3
Jeff Thies wrote:
However, if the result is not naturally an array, consider

... return { A:value_a, B:value_b } }

or Ob = getTwo(some_var)
and then work with Ob.A & Ob.B .


That's really nice! I'm calling a function that calculates many different
properties. Now I can retrieve any of these that are in the hash. Perfect
for astronomical calculations.

BTW. Wanted to thank you for the javascript time/date FAQ. Used it a bunch
yesterday!

Cheers,
Jeff


If you know what you want to return, such as the description of a star, I would
strongly urge you to create an object Star, then return an instance of that
object. I don't know the properties of a star, but the code would look something
like:

function Star(distance, brightness, temperature) {
this.distance = distance;
this.brightness = brightness;
this.temperature = temperature;

this.setDistance = function(d) {
if (d > 1000000000) {
return -1;
} else {
this.distance = d;
return this.distance;
}
}
this.getDistance = function() {
return this.distance + 'au';
}
this.getBrightness = function() {
return this.brightness;
}
this.getTemperature = function() {
return this.temperature;
}
}

Then in your function that returns information about a star, it could do
something like:

return new Star(1, 2, 3);

And the code that uses the returned value would be:

var someStar = someFunctionThatReturnsAStarObject();
document.write('The star is ' + someStar.getDistance() + ' away');
if (someStar.setDistance(100000000000) == -1) {
document.write('Something went wrong setting the star\'s distance');
} else {
document.write('The star is now ' + someStar.getDistance() + ' away');
}

By creating a Star object, you can encapsulate data validation (as in the method
setDistance() and you manipulate the output in a single place (as with adding
'au' to the value returned by getDistance()).

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #4
> If you know what you want to return, such as the description of a star, I
would
strongly urge you to create an object Star, then return an instance of that object. I don't know the properties of a star, but the code would look something like:

function Star(distance, brightness, temperature) {
this.distance = distance;
this.brightness = brightness;
this.temperature = temperature;

this.setDistance = function(d) {
if (d > 1000000000) {
return -1;
} else {
this.distance = d;
return this.distance;
}
}
this.getDistance = function() {
return this.distance + 'au';
}
this.getBrightness = function() {
return this.brightness;
}
this.getTemperature = function() {
return this.temperature;
}
}
I thought of making this more object oriented. Usually when I call the
function I need 2 or 3 of the return values. These values just kind of fall
out as the calculation proceeds. ie: var a may take 100% of the
calculations, var b may take 80%. var c 98%. The calculations are extensive
and I didn't want to redo them.

Perhaps a wrapper around the function that turns it into an object???

Below is the Moon function:

function getMoon(date){
var lo=64.975464;
var Po=349.383063;
var No=151.950429;
var i=5.145396;

var sun=(findSun(date));
var LAMDAsun=sun.LAMDAsun;
var Msun=sun.M;
var D=dayDiff(date);
var l=(13.1763966*D)+lo;
l=setRange(l);
Mm=setRange(l - .1114041 * D - Po);
var N=setRange(No - .0529539 * D);
var C=l-LAMDAsun;
var Ev=1.2739 * Math.sin(degree(2*C - Mm));
var Ae=.1858 * Math.sin(degree(Msun));
var A3=.37 * Math.sin(degree(Msun));
var Mpm=Mm+Ev-Ae-A3;
var Ec=6.2886 * Math.sin(degree(Mpm));
var A4=.214 * Math.sin(degree(2* Mpm));
var lp=l+Ev+Ec-Ae+A4;
var V=.6583 * Math.sin(degree(lp-LAMDAsun)*2);
var lpp=lp+V;

var Np=N + (.16 * Math.sin(degree(LAMDAsun))); // test should be wrong

var y=Math.sin(degree(lpp-Np)) * Math.cos(degree(i));
var x=Math.cos(degree(lpp-Np));
var arctan=Math.atan(y/x)*180/Math.PI;

// arctan amiguity test
if((y>0)&&(x<0)){arctan=arctan+180};
if((y<0)&&(x<0)){arctan=arctan+180};
if((y<0)&&(x>0)){arctan=arctan+360};

var LAMDAm=arctan+Np;
var Bm=Math.asin(Math.sin(degree(lpp-Np))*Math.sin(degree(i)))*180/Math.PI;
var equatorial=eclipticToEquatorial(LAMDAm,Bm);
var Dm=lpp-LAMDAsun; // Dm is the phase of the moon in degrees ~13
degrees/day
var F=(1-Math.cos(degree(Dm)))/2;

return{F:F,Dm:Dm,alpha:equatorial.alpha,delta:equa torial.delta,B:Bm,LAMDA:LA
MDAm};
}

whew... Where do you draw the line between procedural, functional, object
oriented and functional retrieve "hash objects"?

maybe I should have gone to school after OO code was invented. No one ever
asks me to write fortran.

Cheers,
Jeff

Then in your function that returns information about a star, it could do
something like:

return new Star(1, 2, 3);

And the code that uses the returned value would be:

var someStar = someFunctionThatReturnsAStarObject();
document.write('The star is ' + someStar.getDistance() + ' away');
if (someStar.setDistance(100000000000) == -1) {
document.write('Something went wrong setting the star\'s distance');
} else {
document.write('The star is now ' + someStar.getDistance() + ' away');
}

By creating a Star object, you can encapsulate data validation (as in the method setDistance() and you manipulate the output in a single place (as with adding 'au' to the value returned by getDistance()).

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...3/reference/fr
ames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...l_reference_en
try.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
14295
by: Robert Fitzpatrick | last post by:
Can someone point me to some more information or perhaps show an example of returning a recordset from a plpgsql function. I'd like to send an argument or arguments to the function, do some queries...
14
3451
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
9
2042
by: Hank Stalica | last post by:
So I've made a linked list class. I have a function that initiates this class, opens a file, parses it, and then inserts nodes into this temporary class. What I would like to do is then have...
7
2281
by: microsoftboy | last post by:
Hi, Using the following selection criteria, I am able to list the data from mysql db, but I would like to provide the following options for users: Select All Data, Select None. I am not sure if...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.