Parameteroverdracht met call by name

Pagina: 1
Acties:
  • 112 views sinds 30-01-2008
  • Reageer

  • _Spike
  • Registratie: November 2001
  • Laatst online: 01-08 20:34
Een vriend en ik zijn bezig met een document over parameteroverdrachten.
Zo stuitten we op het fenomeen "call by name". In het volgende artikel wordt uitgelegd wat de uitkomst is van een swap-functie bij verschillende soorten parameteroverdracht.

Het call by name gedeelte komen we niet uit. Kan iemand ons uitleggen hoe ze aan die uitkomst komen??


Aanvullende informatie over parameteroverdracht is natuurlijk ook gewenst.
Ja, we hebben menig URL al gevonden met Google, maar als iemand toevallig een hele handige/duidelijke weet mag hij/zij die wel met ons delen natuurlijk :)

I don't suffer from insanity, I enjoy every minute of it !!!


Verwijderd

het gaat dus hier over:
Call by name
A call by name is functionally much like a call by reference, but is slightly different in spirit. Instead of passing the address of a variable as an argument, a language that implements call-by-name (i.e., Algol-60) passes a thunk--a pair of functions that provide read and write access to the variable. In the case of simple variables (i.e., not arrays), call-by-name yields the same results as call-by-reference. When an argument to a function or procedure is an array element, however, its index expression is re-evaluated in the caller's scope every time the parameter is used within the function or procedure. Indeed, the swap example below is perhaps the canonical example of the perils of call-by-name. In fact, with call-by-name it is impossible to implement a "foolproof" version of such a swap function.

The basic difficulty with call-by-name results from the side effects that assignment via thunk can cause. For example, if an array element is passed as an argument, the precise element of the array being referred to can depend on the state; hence, if the state changes, so does the element being referred to. In the example, both i and a[i] are passed as arguments to the same function; then, when i changes, a[i] refers to a different element of the array from that which was (probably) intended.
(voor degene die geen zin hebbe de link te openen)

  • MSalters
  • Registratie: Juni 2001
  • Laatst online: 08:58
Het idee is dat 'y=t' betekent @global_a[@global_x] = t, waarbij de @ staat voor evaluatie op dat punt. De print in swap() laat zien dat @global_x==2, dus het tweede element wordt veranderd.

Man hopes. Genius creates. Ralph Waldo Emerson
Never worry about theory as long as the machinery does what it's supposed to do. R. A. Heinlein


  • Sjnirk
  • Registratie: Maart 2001
  • Laatst online: 27-03-2025

Sjnirk

Hie vrieve

Zoals ik het begrijp heb je call by name en call bij value, bij call by name pakt ie de variabele en gaat daar verder mee werken, bij call by value pakt ie de waarde van de variabele en gaat daar verder mee werken

Bikkelen gebeurt pas na 2:00


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 03-09 13:30

.oisyn

Moderator Devschuur®

Demotivational Speaker

Op donderdag 30 mei 2002 16:50 schreef Sjnirk het volgende:
Zoals ik het begrijp heb je call by name en call bij value, bij call by name pakt ie de variabele en gaat daar verder mee werken, bij call by value pakt ie de waarde van de variabele en gaat daar verder mee werken
ik denk dat je het verwardt met call by reference, waarbij het adres van de variabele wordt doorgegeven ipv de waarde van de variabele

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.