Re: function/procedure arguments
- Posted by ghaberek (admin) Mar 13, 2018
- 1451 views
hi everyone, i know i am a pest by now, but i have another (probably silly) question.
is it possible to manipulate a sequence, object ... of a function/procedure argument(s) directly in order to avoid duplications being passed back/arround?
example:
function tttest(object t) t={} return 1 end procedure sequence s11 = {1,2,3} integer xx = tttest(s11)
This is a good question. Not everyone is aware of how Euphoria works internally. The manual states the following here: 4.2.1.1 procedures (emphasis mine)
A copy of the value of each argument is passed in. The formal parameter variables may be modified inside the procedure but this does not affect the value of the arguments. Pass by reference can be achieved using indexes into some fixed sequence.
Euphoria uses object reference counting with copy-on-write. So if you pass an object to a routine and do not modify the object, it's effectively been passed by reference. If you modify the value then a new object is created in place with the new value.
If you'd like to manage references to objects, then I suggest using the Pseudo Memory library. This allows you to "allocate" an object and then pass around a reference to it while its value lives in the global ram_space sequence.
include std/eumem.e function tttest(integer t) ram_space[t] = {} return 1 end function integer s11 = eumem:malloc() ram_space[s11] = {1,2,3} integer xx = tttest(s11)
-Greg