Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Call-by-value vs. Call-by-copy

Name: Anonymous 2012-08-06 22:47

Call-by-value means that the value itself is passed. If the parameter is immutable, the binding is also immutable. However, if the binding is a reference value, the parameter is also a reference value unless the caller dereferences it.
Look at this Scheme code.
(define (hax anus) (set! anus 'hacked))
(define my-anus 'safe)
(hax my-anus)

Here, my-anus is still safe, and there was no error in the set!. What happened?
#include <stdio.h>
void hax(char *anus) {
     static char *hacked = "hacked";
     anus = hacked;
}
int main(void) {
     char *my_anus = "safe";
     hax(my_anus);
     puts(my_anus);
     return 0;
}

C works the same way. It prints "safe". Here, my_anus is a char * lvalue. anus is also a char * lvalue. They both have the same type meaning no coercions are needed, so why are they not the same value?
It's because Scheme and C aren't call-by-value, but call-by-copy. The procedures each receive a new copy of the value, not the same value that was passed in. Any operations on a parameter actually operate on the copy.
Two languages that are call-by-value are ML and Algol 68. Both use := for assignment, where the level of refs on the left must be one more than the level of refs on the right. Algol 68 uses dereferencing coercions on the right hand side of an assignment. ML uses the ! dereference operator.
PROC hax = (REF STRING anus)VOID: anus := "hacked";
REF STRING my anus = LOC STRING := "safe";
hax(my anus);
print(my anus)

Here, the type of my anus is clearly a REF STRING, as is the type of the parameter. It is passed by value. No coercions are performed except the implicit dereferencing in the call to print.
fun hax anus = anus := "hacked"
val my'anus = ref "safe";
hax my'anus;
val () = print(!my'anus)

It may be harder to tell because of type inference, but my'anus is a string ref, passed by value. The print here uses dereferencing, too, but it's explicit.
Both of these print "hacked".

Name: Anonymous 2012-08-06 23:23

>>11
nice dubz

Name: Anonymous 2012-08-07 5:00

Tl;dr.  I'm getting a sense that you don't really know what you are talking about though

Name: Anonymous 2012-08-07 5:07

In VB classic you would just qualify the anus parameter as ByRef or ByVal.

Name: Anonymous 2012-08-07 11:06

SICP section 3.2 p. 236: The Environment Model of Evaluation.
What does it mean anyway?

Don't change these.
Name: Email:
Entire Thread Thread List