<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.sternwarte.uni-erlangen.de/wiki/index.php?action=history&amp;feed=atom&amp;title=References_to_variables</id>
	<title>References to variables - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?action=history&amp;feed=atom&amp;title=References_to_variables"/>
	<link rel="alternate" type="text/html" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=References_to_variables&amp;action=history"/>
	<updated>2026-04-30T03:28:48Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.35.7</generator>
	<entry>
		<id>https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=References_to_variables&amp;diff=927&amp;oldid=prev</id>
		<title>Lorenz: Created page with &quot;=== References to variables === (after an email by Manfred)  This should explain difference between modifying an array (which is passed by reference into functions) and creati...&quot;</title>
		<link rel="alternate" type="text/html" href="https://www.sternwarte.uni-erlangen.de/wiki/index.php?title=References_to_variables&amp;diff=927&amp;oldid=prev"/>
		<updated>2018-04-12T13:58:10Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=== References to variables === (after an email by Manfred)  This should explain difference between modifying an array (which is passed by reference into functions) and creati...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=== References to variables ===&lt;br /&gt;
(after an email by Manfred)&lt;br /&gt;
&lt;br /&gt;
This should explain difference between modifying an array (which&lt;br /&gt;
is passed by reference into functions) and creating a new array (whose&lt;br /&gt;
scope is only local to your functions and will therefore be&lt;br /&gt;
garbage-collected after the function has exited).&lt;br /&gt;
&lt;br /&gt;
Whenever you modify only elements of an array a, i.e., when you use&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a[where(a&amp;gt;2)] +=1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a[*] +=1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
the reference to the array as a whole is not changed, which means that&lt;br /&gt;
you really modify the original array.&lt;br /&gt;
&lt;br /&gt;
Whenever you assign a new value to the entire variable `a', i.e. for&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a += 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
which is just an abbreviation for&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a = a + 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
or when you make an explict copy:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
a = @a;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
you lose the reference to the original array.&lt;br /&gt;
It doesn't matter that your new value happens to again be an array,&lt;br /&gt;
whose values are closely related to the original array... &lt;br /&gt;
&lt;br /&gt;
Therefore, if you plan on modifying an array that was passed to the function through a qualifier, make sure you actually make a copy of the array&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
variable tim = @qualifier(&amp;quot;tim&amp;quot;,[0.,0.]);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
such that changes are not passed on to the array outside the function. Otherwise, you might be in for an unpleasant surprise. &lt;br /&gt;
&lt;br /&gt;
Dereferencing a reference to a variable or function (the qualifier function in this example) would be:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
variable qref = &amp;amp;qualifier;&lt;br /&gt;
variable bla = (@qref)(&amp;quot;tim&amp;quot;,[0,0]);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The fact that reassigning entire arrays requires the creation of a new&lt;br /&gt;
array, followed by -- if there are no further references to the original&lt;br /&gt;
one -- garbage-collection of the old array (i.e. freeing its memory), is&lt;br /&gt;
the reason why building up arrays in loops is, though convenient, so&lt;br /&gt;
slow (for sufficiently large N) compared to building up lists, and&lt;br /&gt;
should therefore be avoided:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
variable a = Double_Type[0];&lt;br /&gt;
_for i (0, N)   a = [a, f(i)];  % creates a new&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
vs.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
variable l = {};&lt;br /&gt;
_for i (0, N)   list_append(l, f(i));  % work on one list&lt;br /&gt;
variable a = list_to_array(l);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
(If you're interested, see a crux:~hanke/array_copy for a live demo of&lt;br /&gt;
the O(n^2) slowdown of `a=[a,f(i)]'.)&lt;br /&gt;
&lt;br /&gt;
[[Category:Isis / Slang]]&lt;/div&gt;</summary>
		<author><name>Lorenz</name></author>
	</entry>
</feed>