On Fri, Feb 13, 2009 at 4:43 PM, Mark Mandel <[email protected]> wrote:
> In (almost?) every other programming language, everything is passed by
> reference, unless you specify it otherwise.

Not true. Most languages actually have pass-by-value semantics.

For example, C++ added a very specific syntax on top of C
underpinnings to allow users to *choose* to pass by reference because
both C and C++ have pass-by-value semantics. The reason it doesn't
seem like a problem is because in C and C++, an array name is an
lvalue that decays to a pointer rvalue (i.e., char[4] is really char*)
and pointers are passed by value (so char* accepts an array of char).

Pascal has a 'var' keyword to specify pass-by-reference, without it
you have pass-by-value (the default).

Java is also pass-by-value. It just blurs the distinction because
SomeType obj does not really make obj have SomeType, it makes it an
inherent reference to it (strictly a pointer behind the scenes). Read
this article for a fuller explanation of why:

http://javadude.com/articles/passbyvalue.htm

> Pass by value of arrays in CF drives me nuts, but it was an old, old, old
> decision, and it's not going to change.

Technically the semantics are even more complicated. Arrays are
actually copy-on-assignment. When you pass an array to a function, it
assigns the array to the argument, creating a copy. When you return an
array from a function, it does not copy it - unless you assign the
result to a variable. You can see that here:

<cfscript>
a = [ 1, 2, 3, 4 ];

function getA() { return a; }

b = getA(); // copies a so b is a separate array
b[1] = 42; // does not change a

arrayAppend(getA(), 5); // no assignment - a *is* modified
</cfscript>
-- 
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CFCDev" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/cfcdev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to