Debian Codegraph @

help/pt_BR/html/ch07s04.html

raw
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>True Local Variables</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Manual do Genius"><link rel="up" href="ch07.html" title="Chapter 7. Advanced Programming with GEL"><link rel="prev" href="ch07s03.html" title="Returning Functions"><link rel="next" href="ch07s05.html" title="GEL Startup Procedure"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">True Local Variables</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch07s03.html">Prev</a> </td><th width="60%" align="center">Chapter 7. Advanced Programming with GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch07s05.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="genius-gel-true-local-variables"></a>True Local Variables</h2></div></div></div><p lang="en">
2 When passing functions into other functions, the normal scoping of
3 variables might be undesired. For example:
4 </p><pre lang="en" class="programlisting">k := 10;
5 function r(x) = (x+k);
6 function f(g,x) = (
7 k := 5;
8 g(x)
9 );
10 f(r,1)
11 </pre><p lang="en">
12 you probably want the function <code class="function">r</code>
13 when passed as <code class="function">g</code> into <code class="function">f</code>
14 to see <code class="varname">k</code> as 10 rather than 5, so that
15 the code returns 11 and not 6. However, as written, the function
16 when executed will see the <code class="varname">k</code> that is
17 equal to 5. There are two ways to solve this. One would be
18 to have <code class="function">r</code> get <code class="varname">k</code> in a
19 private dictionary using the square bracket notation section
20 <a class="link" href="ch07s03.html" title="Returning Functions">Returning
21 Functions</a>.
22 </p><p lang="en">
23 But there is another solution. Since version 1.0.7 there are
24 true local variables. These are variables that are visible only
25 from the current context and not from any called functions.
26 We could define <code class="varname">k</code> as a local variable in the
27 function <code class="function">f</code>. To do this add a
28 <span class="command"><strong>local</strong></span> statement as the first statement in the
29 function (it must always be the first statement in the function).
30 You can also make any arguments be local variables as well.
31 That is,
32 </p><pre lang="en" class="programlisting">function f(g,x) = (
33 local g,x,k;
34 k := 5;
35 g(x)
36 );
37 </pre><p lang="en">
38 Then the code will work as expected and prints out 11.
39 Note that the <span class="command"><strong>local</strong></span> statement initializes
40 all the referenced variables (except for function arguments) to
41 a <code class="constant">null</code>.
42 </p><p lang="en">
43 If all variables are to be created as locals you can just pass an
44 asterisk instead of a list of variables. In this case the variables
45 will not be initialized until they are actually set.
46 So the following definition of <code class="function">f</code>
47 will also work:
48 </p><pre lang="en" class="programlisting">function f(g,x) = (
49 local *;
50 k := 5;
51 g(x)
52 );
53 </pre><p lang="en">
54 </p><p lang="en">
55 It is good practice that all functions that take other functions
56 as arguments use local variables. This way the passed function
57 does not see implementation details and get confused.
58 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch07s03.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch07.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch07s05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Returning Functions </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> GEL Startup Procedure</td></tr></table></div></body></html>