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.
Here,
C works the same way. It prints "safe". Here,
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
Here, the type of
It may be harder to tell because of type inference, but
Both of these print "hacked".
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".