From: Timo Reitz on
Jorge wrote:
>> <?php $bar = array('b');
>>
>> /* Array ( [0] => b ) */ print_r($bar);
>>
>> /* &$a means pass-by-reference in PHP */ function foo(&$a) { $a =
>> array(); }
>>
>> foo($bar);
>>
>> /* Array ( ) */ print_r($bar); ?>
>
> Here, inside foo, $a is a reference to the variable $bar, not a reference
> to the array object ['b'] that $bar points to, therefore - inside foo-
> $a= array(); is === $bar= array();

This is not true, the PHP manual explicitely states[1]:

"$a =& $b;

$a and $b are completely equal here. $a is not pointing to $b or vice versa.
$a and $b are pointing to the same place."

If $a would only contain a reference to the variable $bar, how would you
explain the following behaviour?

<?php
$a=50;
$b=&$a;
unset($a);
echo $b; // Output: 50
?>

Javascript does not have call by reference (it does have what seems to be
called "call by sharing", which I never heard of before).

[1] http://www.php.net/manual/en/language.references.whatdo.php
From: Jorge on
On Oct 26, 10:57 pm, Timo Reitz <godsb...(a)arcor.de> wrote:
> (...)
> If $a would only contain a reference to the variable $bar, how would you
> explain the following behaviour?
>
> <?php
> $a=50;
> $b=&$a;
> unset($a);
> echo $b; // Output: 50
> ?>

Ok, I see. You're right, that is the case, and here's the explanation:
http://www.php.net/manual/en/language.references.arent.php

Whoever believes *that* to be what pass-by-reference truly means, is
mistaken:

"Call by reference
In call-by-reference evaluation, a function receives an implicit
reference to the argument, rather than a copy of its value. This
typically means that the function can modify the argument- something
that will be seen by its caller. Call-by-reference therefore has the
advantage of greater time- and space-efficiency (since arguments do
not need to be copied), as well as the potential for greater
communication between a function and its caller (since the function
can return information using its reference arguments), but the
disadvantage that a function must often take special steps to
"protect" values it wishes to pass to other functions.
(...)
Even among languages that don't exactly support call-by-reference,
many, including C and ML, support explicit references (objects that
refer to other objects), such as pointers (objects representing the
memory addresses of other objects), and these can be used to effect or
simulate call-by-reference (but with the complication that a
function's caller must explicitly generate the reference to supply as
an argument)."

http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference

Here's what would have happened in C, had $b been a true reference:

int a= 50;
int *b= &a;

printf("%d", *b); //prints 50
a= 0;
printf("%d", *b); //prints 0

> Javascript does not have call by reference

What I have said, and I maintain, is that JS's behaviour when passing
an *object* is identical to that of C when passing an object by
reference, and 100% compatible with what "pass-by-reference" has
always meant. (which is *not* -seemingly- what PHP does)

> (it does have what seems to be
> called "call by sharing", which I never heard of before).

LOL, what a mess:
http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_sharing

But here's the first hint:
"Although this term (call-by-sharing) has widespread usage in the
Python community, identical semantics in other languages (...) are
often described as call-by-value, where the value is implied to be a
*reference* to the object."

And the second hint:
In call-by-reference evaluation, a function receives an implicit
*reference* to the argument, rather than a copy of its value.
--
Jorge.
From: Jorge on
On Oct 27, 2:12 am, Jorge <jo...(a)jorgechamorro.com> wrote:
> (...)
> here's the first hint:
> "Although this term (call-by-sharing) has widespread usage in the
> Python community, identical semantics in other languages (...) are
> often described as call-by-value, where the value is implied to be a
> *reference* to the object."
>
> And the second:
> In call-by-reference evaluation, a function receives an implicit
> *reference* to the argument, rather than a copy of its value.

And the third:
call-by-reference === call-by-value when the value is a reference.
--
Jorge.
From: GodsBoss on
On 27 Okt., 02:12, Jorge <jorge(a)jorgechamorro.com> wrote:
> On Oct 26, 10:57 pm, Timo Reitz <godsboss(a)arcor.de> wrote:
> > <?php
> > $a=50;
> > $b=&$a;
> > unset($a);
> > echo $b; // Output: 50
> > ?>
>
> Ok, I see. You're right, that is the case, and here's the explanation:
> http://www.php.net/manual/en/language.references.arent.php
>
> Whoever believes *that* to be what pass-by-referencetruly means, is
> mistaken:

I don't think so, call-by-reference is exactly what PHP does and it is
not
what C with its pointers does.

> "Call byreference
> In call-by-referenceevaluation, a function receives an implicit
> reference to the argument, rather than a copy of its value. This
> typically means that the function can modify the argument- something
> that will be seen by its caller.
> (...)
> http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference

In PHP, you can do the following:

// Let $a be a variable with any value.
// Let doSomething be a function with call-by-reference.
$b=$a;
doSomething($b);
// Now $a != $b !

This is exactly, what call-by-reference means!
In javascript, you can't do this:

// Let a be a variable with any value.
// Let doSomething be a function.
var b=a;
doSomething(a);
// a===b, in every case.

If javascript supported call-by-reference, you could have changed a in
a manner that a!==b (maybe you could cheat by using something like
Mozilla's
access to variables within closures, but it would not work in vanilla
ECMAScript).

> Here's what would have happened in C, had $b been a truereference:
>
> int a= 50;
> int *b= &a;
>
> printf("%d", *b); //prints 50
> a= 0;
> printf("%d", *b); //prints 0

In PHP, this works exactly the same:

$a=50;
$b=&$a;
$a=0;
// Now $b is 0, too!

But please notice how you use variables of different types in C
(an integer and a pointer to an integer), while in PHP both variables
are of the same type.

And, again, javascript - how would I achieve the same effect here?

> > Javascript does not have call byreference
>
> What I have said, and I maintain, is that JS's behaviour when passing
> an *object* is identical to that of C when passing an object byreference, and 100% compatible with what "pass-by-reference" has
> always meant. (which is *not* -seemingly- what PHP does)

I agree with the part that javascript does what C does, but C does not
have
real call-by-reference either, as is stated in the Wikipedia article
you cited:

"Even among languages that _don't exactly support call-by-reference_,
many,
_including C_ and ML, support explicit references (objects that refer
to
other objects), such as pointers (objects representing the memory
addresses
of other objects), and these can be used to effect or simulate
call-by-reference (but with the complication that a function's caller
must
explicitly generate the reference to supply as an argument)."

> > (it does have what seems to be
> > called "call by sharing", which I never heard of before).
>
> LOL, what a mess:http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_sharing
>
> But here's the first hint:
> "Although this term (call-by-sharing) has widespread usage in the
> Python community, identical semantics in other languages (...) are
> often described as call-by-value, where the value is implied to be a
> *reference* to the object."
>
> And the second hint:
> In call-by-referenceevaluation, a function receives an implicit
> *reference* to the argument, rather than a copy of its value.

You have to distinguish between call-by-reference and call-by-value
where the value
is a reference. They are not the same.
From: Jorge on
On Oct 27, 10:04 am, GodsBoss <godsb...(a)gmail.com> wrote:
> (...)
> I don't think so, call-by-reference is exactly what PHP does and it is
> not what C with its pointers does.
>
> > "Call by reference
> > In call-by-reference evaluation, a function receives an implicit
> > reference to the argument, rather than a copy of its value. This
> > typically means that the function can modify the argument- something
> > that will be seen by its caller.
> > (...)
> >http://en.wikipedia.org/wiki/Pass_by_reference#Call_by_reference
>
> In PHP, you can do the following:
>
> // Let $a be a variable with any value.
> // Let doSomething be a function with call-by-reference.
> $b=$a;
> doSomething($b);
> // Now $a != $b !
>
> This is exactly, what call-by-reference means!

What I would have said as a C programmer is that given the outcome: //
Now $a != $b, $b must have been -necessarily- passed by reference.
(Assuming that neither $a nor $b were already in scope inside
doSomething() in which case they could have been touched directly, or,
assuming that only $b has been modified and it's been touched using no
other means than doSomething()'s first argument.)

> In javascript, you can't do this:
>
> // Let a be a variable with any value.
> // Let doSomething be a function.
> var b=a;
> doSomething(a);
> // a===b, in every case.
>
> If javascript supported call-by-reference, you could have changed a in
> a manner that a!==b (...)

Agreed. I've never said that JS supported call-by-reference, I said
that saying (as in Mozilla's MDC) that "objects are passed by
reference" is correct.

>
> > Here's what would have happened in C, had $b been a true reference:
>
> > int a= 50;
> > int *b= &a;
>
> > printf("%d", *b); //prints 50
> > a= 0;
> > printf("%d", *b); //prints 0
>
> In PHP, this works exactly the same:
>
> $a=50;
> $b=&$a;
> $a=0;
> // Now $b is 0, too!

Ok. Perfect. That's what I would have expected.
How (or why) is that compatible with your previous PHP example in
which the behaviour was (apparently) the opposite ?

> But please notice how you use variables of different types in C
> (an integer and a pointer to an integer), while in PHP both variables
> are of the same type.

That. Yes. And everything is pretty clear and unambiguous when the
referencing and de-referencing operators ["*","->","&"] are used
explicitly :-)

> And, again, javascript - how would I achieve the same effect here?

It's not possible.

> > > Javascript does not have call byreference
> (...)
> You have to distinguish between call-by-reference and call-by-value
> where the value is a reference. They are not the same.

Thanks Timo, I see your point. You've put it very well, really.
Interesting and constructive.

But let me ask you this:

Consider:

/* ****************** JavaScript: */
x= {}; //a reference to {}
o= {}; //a different reference to a different {}
function f (a) {
a.p= 27;
a= x;
a.p= 33;
}
f(o);
console.log([o.p, x.p]); // 27,33

/* ****************** C: */
typedef struct {
int p;
} object;

object xx; //creates 1st {}
object oo; //creates 2nd {}
object *o= &oo; //a reference to 1st {}
object *x= &xx; //a different reference to a different {}

function f (object *a) {
a->p= 27; //or (*a).p= 27;
a= x; //or a= &xx;
a->p= 33; //or (*a).p= 33;
}
f(o); //or f(&oo);
printf("%d,%d",oo.p, xx.p); // 27,33
printf("%d,%d",o->p, x->p); // 27,33

(try to ignore the unavoidable differences in syntax, due to var
typing and explicit de/referencing)

Question # 1: Do you agree that o are references in both programs ?
Question # 2: Do you agree that it's o's value what gets passed (by
copy) to f() ?
Question # 3: Do you agree that both programs produce exactly the same
results ?
Question # 4: What if not a reference could be 'o' in order to produce
these results/behaviour ?

Well. Now ask whoever C programmer you like, how is it called when
'oo' is passed like that. His answer is going to be "it's being passed
by reference". Simply because it's a reference to 'oo' what's being
passed. In JS just like in C, only that in C it's more evident because
of the explicit use of &oo. It has always been called so. What that
article in MDC does is to call it as it's been always called, when
they say that "objects are passed by reference".

Now, somebody may want to lucubrate about other aspects or about the
entire mechanism of parameter passing in these or that dynamic
languages, but not me. That's beyond this simple fact, ISTM.

Cheers,
--
Jorge.