<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Functions: Passing arguments by value or by reference</title>
	<atom:link href="http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference/feed" rel="self" type="application/rss+xml" />
	<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference</link>
	<description>Ruben writes flashy writings</description>
	<pubDate>Thu, 28 Aug 2008 10:14:58 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Ruben</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-415</link>
		<dc:creator>Ruben</dc:creator>
		<pubDate>Sun, 03 Jun 2007 17:43:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-415</guid>
		<description>Alright, actually that was more or less what I meant, I merely put it the wrong way.. Thanks alot for going through the trouble of explaining all of it man..</description>
		<content:encoded><![CDATA[<p>Alright, actually that was more or less what I meant, I merely put it the wrong way.. Thanks alot for going through the trouble of explaining all of it man..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Theo</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-414</link>
		<dc:creator>Theo</dc:creator>
		<pubDate>Sun, 03 Jun 2007 17:28:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-414</guid>
		<description>No, being immutable means that you can't change it at all.

Remember that there is a difference between a variable and an object. A variable is just a named reference to an object, changing the variable just points it to another object (that is what = does, it changes where a variable points).

You can have two variables referencing the same object. Lets say we have "foo" and "bar" both pointing to the same array:

var foo = [1, 2, 3];
var bar = foo;

Now, if you change the array using the foo variable, that change can be seen by inspecting the bar variable too:

foo.pop();

trace(foo.length); // prints 2
trace(bar.length); // prints 2

However, if we do a similar excercise with ints it it doesn't work the same way:

var foo = 5;
var bar = foo;

foo += 3;

trace(foo); // prints 8
trace(bar); // prints 5

This is because there is no way to change the object that was referenced by both foo and bar, if you want something else (like foo + 3) you will get a completely new object.

There are methods on Array that returns new objects, for example concat and slice, so not all operations are equal:

var foo = [1, 2, 3];
var bar = foo;

foo = foo.concat([4, 5]);

trace(foo.length); // prints 5
trace(bar.length); // print 3

What happened in this last example is that the foo variable got overwritten by a new array (the return value of the concat call), but bar still pointed at the old array.</description>
		<content:encoded><![CDATA[<p>No, being immutable means that you can&#8217;t change it at all.</p>
<p>Remember that there is a difference between a variable and an object. A variable is just a named reference to an object, changing the variable just points it to another object (that is what = does, it changes where a variable points).</p>
<p>You can have two variables referencing the same object. Lets say we have &#8220;foo&#8221; and &#8220;bar&#8221; both pointing to the same array:</p>
<p>var foo = [1, 2, 3];<br />
var bar = foo;</p>
<p>Now, if you change the array using the foo variable, that change can be seen by inspecting the bar variable too:</p>
<p>foo.pop();</p>
<p>trace(foo.length); // prints 2<br />
trace(bar.length); // prints 2</p>
<p>However, if we do a similar excercise with ints it it doesn&#8217;t work the same way:</p>
<p>var foo = 5;<br />
var bar = foo;</p>
<p>foo += 3;</p>
<p>trace(foo); // prints 8<br />
trace(bar); // prints 5</p>
<p>This is because there is no way to change the object that was referenced by both foo and bar, if you want something else (like foo + 3) you will get a completely new object.</p>
<p>There are methods on Array that returns new objects, for example concat and slice, so not all operations are equal:</p>
<p>var foo = [1, 2, 3];<br />
var bar = foo;</p>
<p>foo = foo.concat([4, 5]);</p>
<p>trace(foo.length); // prints 5<br />
trace(bar.length); // print 3</p>
<p>What happened in this last example is that the foo variable got overwritten by a new array (the return value of the concat call), but bar still pointed at the old array.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ruben</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-411</link>
		<dc:creator>Ruben</dc:creator>
		<pubDate>Sun, 03 Jun 2007 11:24:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-411</guid>
		<description>So a type being immutable basically means that its value can only be changed by using the  &lt;em&gt;=&lt;/em&gt;  operator..
Knowing this, it indeed makes a bit more sense why simple types behave as being values instead of references..

I really appreciate that you took the time to post that, so many thanks for the explanation!</description>
		<content:encoded><![CDATA[<p>So a type being immutable basically means that its value can only be changed by using the  <em>=</em>  operator..<br />
Knowing this, it indeed makes a bit more sense why simple types behave as being values instead of references..</p>
<p>I really appreciate that you took the time to post that, so many thanks for the explanation!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Theo</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-403</link>
		<dc:creator>Theo</dc:creator>
		<pubDate>Sat, 02 Jun 2007 15:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-403</guid>
		<description>Take the example of Array vs. String: arrays are mutable, i.e have methods that change the same instance as they operate on (push, pop, shift, unshift, for example). Strings are immutable, however, it has no methods that change the instance they operate on, only methods that return a new string. The same applies to int, Number and Boolean.

If you pass an array to a method and that method changes the array (by calling pop, for example), you can determine if the array was passed by reference or by value depending on whether or not the array was changed in both scopes, or just in the called method's scope.

If you instead pass a string to a method it doesn't matter whether that string is passed by value or by reference, because there is nothing you can do to prove that it was passed one way or the other. You cannot change the instance, so every copy of the instance will be equal.</description>
		<content:encoded><![CDATA[<p>Take the example of Array vs. String: arrays are mutable, i.e have methods that change the same instance as they operate on (push, pop, shift, unshift, for example). Strings are immutable, however, it has no methods that change the instance they operate on, only methods that return a new string. The same applies to int, Number and Boolean.</p>
<p>If you pass an array to a method and that method changes the array (by calling pop, for example), you can determine if the array was passed by reference or by value depending on whether or not the array was changed in both scopes, or just in the called method&#8217;s scope.</p>
<p>If you instead pass a string to a method it doesn&#8217;t matter whether that string is passed by value or by reference, because there is nothing you can do to prove that it was passed one way or the other. You cannot change the instance, so every copy of the instance will be equal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ruben</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-392</link>
		<dc:creator>Ruben</dc:creator>
		<pubDate>Fri, 01 Jun 2007 16:53:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-392</guid>
		<description>Hey Theo, thanks for the feedback, would you care to explain what exactly you meant with what you said?</description>
		<content:encoded><![CDATA[<p>Hey Theo, thanks for the feedback, would you care to explain what exactly you meant with what you said?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Theo</title>
		<link>http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-390</link>
		<dc:creator>Theo</dc:creator>
		<pubDate>Fri, 01 Jun 2007 15:17:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.rubenswieringa.com/blog/functions-passing-arguments-by-value-or-by-reference#comment-390</guid>
		<description>On the other hand, primitives are immutable, so you would never know the difference either way.</description>
		<content:encoded><![CDATA[<p>On the other hand, primitives are immutable, so you would never know the difference either way.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
