Pass by reference or by value?

Steve Holden steve at holdenweb.com
Mon Jul 16 13:55:26 EDT 2007


Beliavsky wrote:
> On Jul 13, 11:58 pm, sturlamolden <sturlamol... at yahoo.no> wrote:
> 
> <snip>
> 
>> In Fortran you can only pass references.
>>
>> integer(4) :: a
>> a = 1
>> call bar(a)
>>
>> subroutine bar(a)
>> integer(4) :: a
>> a = 0             ! side-effect
>> end subroutine
>>
>> That means, when a variable is used to call a function, the function
>> receives a pointer to the actual argument, not a local copy. That is
>> very different from C's copy-passing behaviour.
> 
> In Fortran, if a procedure argument is modified within the procedure,
> that change is propagated to the value of the argument in the caller.
> The standard does NOT mandate how this is accomplished, and one could
> in theory write a compiler that makes a local copy of all procedure
> arguments, as long as the variables passed as arguments were updated
> in the caller. Early implementations of Fortran 90 often made copies
> of array arguments, hurting performance. Current compilers do this
> less often.
> 
> It is common to pass constants and expressions to Fortran procedures,
> which does not fit the pass-by-reference paradigm.
> 
You can pass a reference to a constant just as easily as you can pass a 
reference to a variable. The only think you have to ensure is that when 
you pass a reference to a constant something stops the procedure from 
changing it.

Some early Fortran compilers omitted this small but important detail, 
leading to programs with incomprehensible semantics, as the values of 
numeric literals could no longer be relied upon.

> Fortran 2003 has the VALUE attribute to give the C pass-by-value
> behavior when desired.
> 
regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC/Ltd           http://www.holdenweb.com
Skype: holdenweb      http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------




More information about the Python-list mailing list