Re: function/procedure arguments

new topic     » goto parent     » topic index » view thread      » older message » newer message

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 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu