1. function/procedure arguments
- Posted by begin Mar 13, 2018
- 1159 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)
thanks richard
2. Re: function/procedure arguments
- Posted by ghaberek (admin) Mar 13, 2018
- 1157 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
3. Re: function/procedure arguments
- Posted by begin Mar 13, 2018
- 1169 views
thanks very much greg - learned a lot.
trying your code, i found with phix i get:
H:\Euphoriaprog\Microexp\tester.ew:8 type check failure, s11 is 4.611686019e+18 Global & Local Variables --> see H:\Euphoriaprog\Microexp\ex.err Press Enter...
changing
integer s11 = eumem:malloc()
to
integer s11 = eumem:malloc(1,1)
the example works for phix
richard
4. Re: function/procedure arguments
- Posted by _tom (admin) Mar 13, 2018
- 1128 views
Here is something that is missing from the documentation. However, the answer you wanted comes from Greg.
_tom
Passing Values to a Routine
A parameter is "a variable you declare in the signature of a routine." An argument is "is the something you pass to a parameter when you call a routine."
( Note that authors are always getting parameter and argument confused. Some authors resort to calling an argument a formal parameter. )
You can imagine three ways of passing the something to a parameter:
- pass by value
- pass by reference
- pass by elision
The question is, what does Euphoria do?
Pass by value (Euphoria just looks like this)
The parameter is a full copy of the argument. That means more memory and time for the copy operation. Changes to the parameter do not change the original argument value.
Pass by reference (Never in Euphoria)
The parameter becomes a reference back to the argument. Copying just a reference needs no extra memory and is very fast. Changes to the parameter also changes the original argument value.
Pass by elision (Euphoria does this)
The parameter receives a reference to the argument. Copying just a reference needs no extra memory and is very fast. The trick in Euphoria languages is that changes to the original arguments are not allowed. Changes to a parameter are localized to the routine--they do not alter the original. In Euphoria the elision of an argument means copy just what you need and reference everything else.
The word elision means "omission by intent or design." Thus elision is the omission of a sound when speaking. Instead of I am you say I'm. Instead of let us you say let's.
In Euphoria elision means:
sequence s = repeat( 0, 100_000_000 ) --(1) a huge sequence -- with many many items function foo( object x ) --(2) `looks-like` a huge copy job is needed -- actually `x` will be a reference to `s` x[1] += 1 --(3) a parameter change now requires -- some local memory, not much though return x[1] --(4) original argument is not changed --(5) cost (time and memory) for a local change is negligible end function ? foo( s ) --> 1 --(6) pass-by-elision explains why Euphoria is -- * safe -- * fast
5. Re: function/procedure arguments
- Posted by petelomax Mar 13, 2018
- 1093 views
NO: You can emulate pass by reference by using an index, but otherwise OE/phix do NOT support that behaviour.
(The technical/confusing thing is: Except for integers OE/phix always passes by reference, but with copy-on-write semantics.)
(Also, although it seems to work that way, ie actual arguments left unaltered, OE/phix don't actually have have pass-by-value at all, except for integers, that aren't updated anyway.)
I was expecting something more like:
?join({"abc", "def", "ghi"}, ", ") -- shows "abc, def, ghi" ?join({"abc", "def", "ghi"}) -- shows "abc def ghi", as the default delimiter is a single space
not that I am a fan of using the term "pass by elision", however.
Pete
6. Re: function/procedure arguments
- Posted by _tom (admin) Mar 13, 2018
- 1120 views
not that I am a fan of using the term "pass by elision", however.
Pete
Work in progress...
I did a quick edit on the previous entry.
The word "elision" shows up in compiler optimizations that seem to work like Euphoria.
Yes, elision is not a great word.
- Euphoria is not pass-by-value (but it looks like it)
- Euphoria is not pass-by-reference (but does uses references)
- Euphoria is pass by [ insert best word ... ]
Everyone, give me a new word.
Emulating pass-by-reference is a different topic
_tom
7. Re: function/procedure arguments
- Posted by petelomax Mar 13, 2018
- 1068 views
( Note that authors are always getting parameter and argument confused. Some authors resort to calling an argument a formal parameter. )
That be wrong too.
The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.
Pete
8. Re: function/procedure arguments
- Posted by petelomax Mar 13, 2018
- 1062 views
Everyone, give me a new word.
I just use "defaulted parameters".
9. Re: function/procedure arguments
- Posted by _tom (admin) Mar 13, 2018
- 1043 views
Everyone, give me a new word.
I just use "defaulted parameters".
"Defaulted parameters" is a different topic.
I want a word for looks-like: pass-by-value, not pass by reference...but uses references and copies when needed... with write-on-copy behavior when needed
_tom
10. Re: function/procedure arguments
- Posted by _tom (admin) Mar 13, 2018
- 1076 views
( Note that authors are always getting parameter and argument confused. Some authors resort to calling an argument a formal parameter. )
That be wrong too.
The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.
Pete
I have seen all kinds of variations of how parameter is used. These authors have not looked at wikipedia.
In the days before computers it was clear that an argument to a function was the supplied value:
sin(x)
`x` is the argument to a trigonometric function
That leaves parameter as the variable defined in a function signature.
As a supporter of Luddites I use this as my starting point.
_tom
11. Re: function/procedure arguments
- Posted by begin Mar 13, 2018
- 1195 views
whow, that is all great, learned a lot. pete, keep the wording as is - it's the explanation that counts. put it into help.
richard