1. Functions vs. parameters
Hi! I was hoping someone could save me some time before I go write
myself some code:
If I pass a parameter to a procedure, is it passed by value or by
reference (of course its transparent to us, but see next question)?
If I pass a parameter to a function, and the same parameter is returned
by that function, what is the overhead cost associated with it, i.e. am I
wasting time copying the object into memory? Is a copy of that object
actually sent? I would assume so, because if a function acts on a
parameter and doesn't return it, the original copy is unchanged.
Reason:
I am creating an include file and I am seriously considering treating the
include file as an object, i.e. all variables are scoped to the file, and the
"member" functions are the functions/procedures, where the global ones
are the "methods" of the object(file). If I do this, then all I need are
procedures to operate on the really, really big data structures that I
*was* previously passing to and returning from functions. Then I can
use functions to return the interesting parts of those structures to the
calling program. Now, the whole reason I am doing this is to try and cut
some time, but am I just totally pointing in the wrong direction here?
My whole idea is to avoid any overhead associated with passing a very
large structure back and forth from function to function.
I appreciate any information...
Thanks,
-Jay
2. Re: Functions vs. parameters
Jay Turley writes:
> If I pass a parameter to a function, and the same parameter
> is returned by that function, what is the overhead cost associated
> with it, i.e. am I wasting time copying the object into memory?
> Is a copy of that object actually sent? I would assume so, because
> if a function acts on a parameter and doesn't return it, the original
> copy is unchanged.
The way things work internally is pretty simple.
Euphoria only goes to the trouble of making a copy of a
sequence when it's really necessary.
If you pass a sequence s to a function foo:
s = {1,2,3,{4,5,6,7}}
x = foo(s)
only a pointer to s is passed. The elements
contained in s are *not* copied. Suppose foo() looks like:
function foo(sequence t)
return t[1]
end function
then while foo() is executing, t and s point at the same data in memory.
Since t is not modified, there is never any reason to make
a copy. If however you have something like:
function foo(sequence t)
t[3] = 99
return t
end function
then at the point that t[3] is set to 99 it will no longer
be possible for t and s to point at the same data
in memory - the interpreter will create a new
sequence in memory {1,2,99,{4,5,6,7}} and t will point to it,
while s continues to point at the original sequence.
Note that the {4,5,6,7} element of s will *not* be copied.
Only the "top-level" elements need to be copied.
A single copy of {4,5,6,7} will be pointed to from s[4] and t[4].
Regards,
Rob Craig
Rapid Deployment Software
http://members.aol.com/FilesEu/