Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4679

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>local variables in a function</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_making_spfunctions_spwith_spa_spvariable_spnumber_spof_sparguments.html">next</a> | <a href="_making_spfunctions.html">previous</a> | <a href="_making_spfunctions_spwith_spa_spvariable_spnumber_spof_sparguments.html">forward</a> | <a href="_making_spfunctions.html">backward</a> | <a href="___The_sp__Macaulay2_splanguage.html">up</a> | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<div><a href="index.html" title="">Macaulay2Doc</a> > <a href="___The_sp__Macaulay2_splanguage.html" title="">The Macaulay2 language</a> > <a href="_local_spvariables_spin_spa_spfunction.html" title="">local variables in a function</a></div>
<hr/>
<div><h1>local variables in a function</h1>
<div>A local variable in a function is one that is not visible to code in other locations.  Correct use of local variables is important, for data stored in global variables will stay around forever, needlessly occupying valuable memory space.  For recursive functions especially, it is important to use local variables so that one invocation of the function doesn't interfere with another.<p/>
The simplest way to create a local variable in a function is with the assignment operator <a href="__co_eq.html" title="assignment of method or new local variable">:=</a>.  The expression <tt>X := Y</tt> means that the value of <tt>Y</tt> will be assigned to a newly created local variable whose name is <tt>X</tt>.  Once <tt>X</tt> has been created, new values can be assigned to it with expressions like <tt>X = Z</tt>.<table class="examples"><tr><td><pre>i1 : i = 22;</pre>
</td></tr>
<tr><td><pre>i2 : f = () -> (i := 0; while i&lt;9 do (&lt;&lt; i &lt;&lt; " "; i=i+1); &lt;&lt;endl;)

o2 = f

o2 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i3 : f()
0 1 2 3 4 5 6 7 8 </pre>
</td></tr>
<tr><td><pre>i4 : i

o4 = 22</pre>
</td></tr>
</table>
In the example above, we see that the function <tt>f</tt> does its work with a local variable <tt>i</tt> that is unrelated to the global variable <tt>i</tt> introduced on the first line.<p/>
In the next example, we show that the local variables in two invocations of a function don't interfere with each other.  We write a function <tt>f</tt> that returns a newly created counting function.  The counting function simply returns the number of times it has been called.<table class="examples"><tr><td><pre>i5 : f = () -> (i := 0; () -> i = i+1)
--warning: function f redefined

o5 = f

o5 : FunctionClosure</pre>
</td></tr>
</table>
Let's use <tt>f</tt> to create counting functions and show that they operate independently.<table class="examples"><tr><td><pre>i6 : p = f()

o6 = p

o6 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i7 : q = f()

o7 = q

o7 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i8 : p(),p(),p(),p(),q(),p(),p(),q(),p(),p()

o8 = (1, 2, 3, 4, 1, 5, 6, 2, 7, 8)

o8 : Sequence</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>