1. Parameters by Reference
Hi to all:
I would like to know if there is any way in EU, to
use parameters by reference in a Procedure call.
( My intention is to translate a Pascal program to
Euphoria, that uses a lot of VAR statements in the
parameters list )
Thanx
Hugo
<herm at peru.itete.com.pe>
2. Re: Parameters by Reference
No, there is no way to pass parameters by reference in
Euphoria.
You can only get around it by making the variables you
want to pass be local (or global) and have the routine
modify them directly. For example:
-- YOU CAN'T DO THIS
procedure increment (atom x)
x = x + 1
end procedure
procedure main ()
integer number_to_increment
number_to_increment = 5
...
increment (number_to_increment)
? number_to_increment -- this will display "5"
end procedure
-- YOU MUST DO THIS
integer increment_variable
procedure increment ()
increment_variable = increment_variable + 1
end procedure
procedure main ()
number_to_increment = 5
...
increment _variable = number_to_increment
increment ()
number_to_increment = increment_variable
? number_to_increment -- this will display "6"
end procedure
This is messy, but it's the only way to modify a
variable in a subroutine.
Rod Jackson
----------
From: Hugo Rozas[SMTP:hrm at PERU.ITETE.COM.PE]
Sent: Wednesday, April 28, 1999 9:38 AM
To: EUPHORIA at LISTSERV.MUOHIO.EDU
Subject: Parameters by Reference
Hi to all:
I would like to know if there is any way in EU, to
use parameters by reference in a Procedure call.
( My intention is to translate a Pascal program to
Euphoria, that uses a lot of VAR statements in the
parameters list )
Thanx
Hugo
<herm at peru.itete.com.pe>
3. Re: Parameters by Reference
You could also use pointers. They aren't in standard Euphoria, but I
wrote some routines for them. You can find them in the archive.
EUPHORIA at LISTSERV.MUOHIO.EDU wrote:
> Hi to all:
>
> I would like to know if there is any way in EU, to
> use parameters by reference in a Procedure call.
> ( My intention is to translate a Pascal program to
> Euphoria, that uses a lot of VAR statements in the
> parameters list )
>
> Thanx
>
> Hugo
> <herm at peru.itete.com.pe>
--
Jeffrey Fielding
JJProg at cyberbury.net
http://members.tripod.com/~JJProg/
4. Re: Parameters by Reference
Roderick Jackson wrote:
>No, there is no way to pass parameters by reference in
>Euphoria.
>
>You can only get around it by making the variables you
>want to pass be local (or global) and have the routine
>modify them directly. For example:
[snip]
> integer increment_variable
>
> procedure increment ()
> increment_variable = increment_variable + 1
> end procedure
>
> procedure main ()
> number_to_increment = 5
> ...
> increment _variable = number_to_increment
> increment ()
> number_to_increment = increment_variable
> ? number_to_increment -- this will display "6"
> end procedure
>
>This is messy, but it's the only way to modify a
>variable in a subroutine.
Not the *only* way. You could change the procedure into a function, and have
it return the modified parm value. For example:
function increment(atom x)
return x + 1
end function
procedure main()
integer number
number = 5
...
number = increment(number)
? number -- this will display "6"
end procedure
If you need to modify multiple parms, the function can return a sequence of
values. For example:
function whatever(atom x, sequence s)
return {x + 1, upper(s)}
end function
procedure main()
integer number
sequence name, tmp
number = 5
name = "Fred"
...
tmp = whatever(number, name)
number = tmp[1] -- number now contains 6
name = tmp[2] -- name now contains "FRED"
end procedure
IMHO, this is much tidier than the local-variable solution.
Be seeing you,
Gabriel Boehme
------
Any culture whose artists are directed or controlled by commercial interests
is in mortal danger.
Robert Fripp
------
5. Re: Parameters by Reference
The best way, IMHO, when you want several parts of your program to share data,
independent of the 'namespace', you could use
special functions:
These examples are key-association libraries, but you could write any sort of
'data'-management-library.
The first version uses 'host'-storage, the second uses 'client'-store, I will
discuss the advantages and disadvantages,
afterwards, of the two approaches.
You use the key-association libs, as following:
-- To store a new value
store_key ("mykey", "myval")
-- To set an already existing key to another value
store_key ("mykey", "myotherval")
-- To retrieve a value
puts (1, retrieve_key ("mykey") )
-- To 'delete' a key
store_key ("mykey", -1)
-- Notice the special value -1.
-- This value will be returned by retrieve_key when no key matches, and setting
a key to this value will 'delete' the key.
-- This is not a problem since the value returned by retrieve_key _will_ return
the correct value.
-- keys_ex_a.e
global constant
TRUE = 1,
FALSE = 0,
NONE = -1
sequence
keys,
data
global procedure store_key (object key, object val)
integer place
place = find (key, keys)
if place then
if equal (val, -1) then
data = data[1..place-1] & data[place+1..length(data)]
keys = keys[1..place-1] & keys[place+1..length(keys)]
else
data[place] = val
end if
elsif equal (val, -1)
data = append(data, val)
keys = append(keys, key)
end if
end procedure
global function retrieve_key (object key)
integer place
place = find (key, keys)
if place then
return data[place]
else
return -1
end if
end function
--- This second example works like this:
-- Creating a new data-base
keydb my_kl
my_kl = new_keydb
-- Storing a new key
my_kl = store_key (my_kl, "mykey", "myval")
-- Setting that key to a new value
my_kl = store_key (my_kl, "mykey", "myotherval")
-- 'Deleting' that key
my_kl = store_key (my_kl, "mykey", NONE) -- none equals -1
-- Retrieving a key
puts (1, retrieve_key (my_kl, "mykey") )
----- keys_ex_b.e -- Client-side data storage
global constant
TRUE = 1,
FALSE = 0,
NONE = -1
type keydb (object x)
if sequence (x) and length(x) = 2 and sequence (x[1]) and sequence
(x[2]) then
return length(x[1]) = length(x[2])
else
return 0
end if
end type
global function new_keydb ()
return {{},{}}
end function
global function store_key (keydb kl, object key, object val)
sequence keys, data
integer place
keys = kl[1]
data = kl[2]
place = find (key, keys)
if place then
if equal (val, -1) then
data = data[1..place-1] & data[place+1..length(data)]
keys = keys[1..place-1] & keys[place+1..length(keys)]
else
data[place] = val
end if
elsif equal (val, -1)
data = append(data, val)
keys = append(keys, key)
end if
return {keys, data}
end function
global function retrieve_key (keydb kl, object key)
sequence data, keys
integer place
keys = kl[1]
data = kl[2]
place = find (key, keys)
if place then
return data[place]
else
return -1
end if
end function
--------------------------------------------------------------------------------------------------------------------------------
------------
Advantages of the server-side storage (ps. these terms are made up by me, there
are not official technical terms, or anything):
+ There are no namespace problems.
+ It easier to use
Advantages of the client-side storage:
+ You can have multiple databases
+ You can store a database, print it out, or store it inside any type of
structure, and therefor you could say, you get a
lot more control over the data. You can copy two databases, etc.
+ Different libraries support the same datatype can work with the data in
the same way the library does. (that is, change
the variable and return it), you wouldn't notice in difference in handling.
And off course all of 'advantages' could be achieved by expending the first
example, with many more routines to clear the
database, create and setup multiple databases, to store a database, etc. However
the initiative can only come from the original
library, any user can not later on, add routines to his program that do this,
since he can not access the variable at all.
Ralf N.
nieuwen at xs4all.nl
ralf_n at email.com
6. Re: Parameters by Reference
Thanks to Roderick, Jeffrey, Boehme and Ralf for his
answers.
Hugo
<herm at peru.itete.com.pe>