473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Returning multiple results from a subroutine ?


What are the best ways of returning multiple results from a subroutine ?

I've been using
... return [a, b, c] }
which is inelegant.

I'm used to Pascal's
procedure X(const A, B : integer; var C, D : byte) ;
where A, B are inputs only, and C, D are in/out.

The needed parameters tend to be numbers, and don't need to be objects.

... return {aa:a, bb:b, cc:c} } // seems OK

but with that the parameter names aa, bb, cc are not visible in the
calling statement, which is a defect.

I'm specifically trying to extend, neaten, & improve <URL:http://www.mer
lyn.demon.co.uk/js-date5#DDTA>.

--
© 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> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #1
7 3815
You could pass in a function pointer to a callback.

You could implement your method as a mutator on a javascript class
(the new internal state would be your return values).

hope that helps,
Ron
Jul 20 '05 #2
b0*****@yahoo.com (asdf asdf) writes:
You could pass in a function pointer to a callback.
Ah, Continuation Passing Style :) My first thought too, but not something
I would recommend in a language without tail-call optimization.
You could implement your method as a mutator on a javascript class
(the new internal state would be your return values).


If you want to emulate call-by-name (the "var" parameter of Pascal or
&-parameter of C++), you can pass a return-object, and set the values
as propertie of it, instead of creating the object in the called
function. It is the closest to a Pascal "var"-parameter that I can
find.

I miss the tuples and simultaneous assignments of languages like SML
and Perl. There is no equivalent in Javascript. The closest would be
a returning an object and using a "with" statement.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
It may be less elegant than what you have now, but you could put all
the numbers into an array. Then just pass a single variable (the array
name) in and out of the sub-routine and access the elements of the
array as required.

David
Jul 20 '05 #4
rh
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<65**********@hotpop.com>...
b0*****@yahoo.com (asdf asdf) writes:

I miss the tuples and simultaneous assignments of languages like SML
and Perl. There is no equivalent in Javascript. The closest would be
a returning an object and using a "with" statement.

Or, at the risk of offending those who so far only moderately despise
"with", and highly despise "eval", you could alternatively re-manifest
the return object properties into caller variables by:

for (var j in retObj) eval(j +"=retObj['"+j+"']");

which might come a little closer to call by name than the use of
"with". That's in the vein of a somewhat neutral observation, as
opposed to a recommendation, however.

(By the way, I fully concur with the attempt to stamp out the evil of
[completely unnecessary use of] eval. Nonetheless, I have run into
cases where only the name of a variable is known, and in the absence
of an available reference, have taken the reasonable course and
eval'ed in order to assign/retrieve a value. If there's a good
alternative, the doh'lt is yet to discover it.)

../rh
/L

Jul 20 '05 #5
> (By the way, I fully concur with the attempt to stamp out the evil of
[completely unnecessary use of] eval. Nonetheless, I have run into
cases where only the name of a variable is known, and in the absence
of an available reference, have taken the reasonable course and
eval'ed in order to assign/retrieve a value. If there's a good
alternative, the doh'lt is yet to discover it.)


var global = this; // This is more portable than relying on 'window'.
....
var variable_name = "foo";
var value = global[variable_name];

http://www.crockford.com/javascript/remedial.html

Jul 20 '05 #6
co********@yahoo.ca (rh) writes:
Or, at the risk of offending those who so far only moderately despise
"with", and highly despise "eval",
That would be me :)
you could alternatively re-manifest the return object properties
into caller variables by:

for (var j in retObj) eval(j +"=retObj['"+j+"']");
This differs from using "with" in two ways:

- You only get the values of the enumerable properties of retObj.
Using "with" makes all the properties visible. That is not so much of
a difference, since the non-enumerable properties of a new object (the
ones in Object.prototype) are already visible as properties of the
global object.

- It assigns the values to local variables if they exist, but those
properties that don't have a local variable of the same name are
created as global variables. With a "with", they are all made local
variables, but will shadow preexisting local variables of the same
name.

This is actually a use for eval that I can't find a way around:
setting the value of a local variable given its name as a string.
It is also bad coding practice. Local variables should be free to
be renamed without changing the how the program works, but this
code will let a function from anywhere in the program change a local
variable by name. I recommend against it.

I would recommend returning an array and have the caller decide which
variables to assign to, or returning an object, and letting the caller
decide how to access the properties.
which might come a little closer to call by name than the use of
"with".
Maybe a little closer, but it is not call-by-name. It is
call-by-copy-restore, but I think that is the best we can get anyway.
That's in the vein of a somewhat neutral observation, as
opposed to a recommendation, however.
:)
(By the way, I fully concur with the attempt to stamp out the evil of
[completely unnecessary use of] eval. Nonetheless, I have run into
cases where only the name of a variable is known, and in the absence
of an available reference, have taken the reasonable course and
eval'ed in order to assign/retrieve a value. If there's a good
alternative, the doh'lt is yet to discover it.)


If the variable is a local variable, then I can't see another way,
since we don't have a reference to the activation object. I highly
recommend not to throw around the names of local variables, though.
Make the global if someone outside of the scope needs to have access
to them.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #7
rh
Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<n0**********@hotpop.com>...
co********@yahoo.ca (rh) writes:
Or, at the risk of offending those who so far only moderately despise
"with", and highly despise "eval",
That would be me :)


And me :)
you could alternatively re-manifest the return object properties
into caller variables by:

for (var j in retObj) eval(j +"=retObj['"+j+"']");
This differs from using "with" in two ways:

- You only get the values of the enumerable properties of retObj.
...


While that may be a difference in the use of "with", my assumption was
that retObj would be generated as an object literal as the return
expression in the called function. As such, there wouldn't be any
non-enumerable properties.
- It assigns the values to local variables if they exist, but those
properties that don't have a local variable of the same name are
created as global variables. With a "with", they are all made local
variables, but will shadow preexisting local variables of the same
name.

See below regarding restriction.
This is actually a use for eval that I can't find a way around:
setting the value of a local variable given its name as a string.
Thanks for confirming.
It is also bad coding practice. Local variables should be free to
be renamed without changing the how the program works, but this
code will let a function from anywhere in the program change a local
variable by name. I recommend against it.
The eval is in the calling function, so the only functions that could
change local variables within are those where there is an explicit
intention to allow it. It would be trivial to restrict the variables
in the caller that would be open to change by the callee (by evaling
on the restricted set, as opposed to the property names provided by
retObj). If you don't want to trust, you don't have to.

I would recommend returning an array and have the caller decide which
variables to assign to, or returning an object, and letting the caller
decide how to access the properties.

I tend to use an array if there are two or three return values. More
than that, I create an object literal and access the properties.
which might come a little closer to call by name than the use of
"with".


Maybe a little closer, but it is not call-by-name. It is
call-by-copy-restore, but I think that is the best we can get anyway.
That's in the vein of a somewhat neutral observation, as
opposed to a recommendation, however.


:)


:)
If the variable is a local variable, then I can't see another way,
since we don't have a reference to the activation object. I highly
recommend not to throw around the names of local variables, though.
Make the global if someone outside of the scope needs to have access
to them.
As pointed out above, you don't really have to "throw around the
names". I hold a similar aversion to putting things into the global
namespace -- seems to me there's already too much potential for name
conflict and side-effects there.

Just trying to hold your feet to the fire a bit here. ;)

../rh

/L

Jul 20 '05 #8

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

Similar topics

1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
13
by: NM | last post by:
Sometimes ago I was having a problem in linking between C++ and Fortran program. That was solved (using input from this newsgroup) using the Fortran keyword "sequence" with the derived types (to...
6
by: lenny | last post by:
Hi, I've been trying to use a Sub or Function in VBA to connect to a database, make a query and return the recordset that results from the query. The connection to the database and the query...
6
by: InnoCreate | last post by:
Hi everyone. I've recently written a classic asp website which uses an MS Access datasource. I know this is less than an ideal data source as it has limited functionality. I have a search form on...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
1
by: ashokbio | last post by:
How to return multiple values by passing multiple arguments in function or subroutine using VB6? T. Ashok Kumar
1
by: Kjell Weding | last post by:
I have this code in VB.NET (VS 2005): Module kvitteringFunk Public Sub skrivKvittering() Dim KvittPrt As String = "" Dim iType As Byte = 0 KvittPrtType(iType, KvittPrt) ' iType should now...
1
by: Max58kl | last post by:
Hi I am trying to setup a form that automatically sends the form values via email to a specific address. I have uploaded the script, which when I run loads a page with the following error...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.