Debian Codegraph @

help/pt_BR/html/ch06s05.html

raw
1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Global Variables and Scope of Variables</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Manual do Genius"><link rel="up" href="ch06.html" title="Chapter 6. Programming with GEL"><link rel="prev" href="ch06s04.html" title="Comparison Operators"><link rel="next" href="ch06s06.html" title="Parameter variables"></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">Global Variables and Scope of Variables</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s04.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Programming with GEL</th><td width="20%" align="right"> <a accesskey="n" href="ch06s06.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-variables-global"></a>Global Variables and Scope of Variables</h2></div></div></div><p lang="en">
2 GEL is a
3 <a class="ulink" href="https://en.wikipedia.org/wiki/Scope_%28programming%29" target="_top">
4 dynamically scoped language</a>. We will explain what this
5 means below. That is, normal variables and functions are dynamically
6 scoped. The exception are
7 <a class="link" href="ch06s06.html" title="Parameter variables">parameter variables</a>,
8 which are always global.
9 </p><p lang="en">
10 Like most programming languages, GEL has different types
11 of variables. Normally when a variable is defined in a function,
12 it is visible from that function and from all functions that are
13 called (all higher contexts). For example, suppose a function
14 <code class="function">f</code> defines a variable <code class="varname">a</code>
15 and then calls function <code class="function">g</code>. Then
16 function <code class="function">g</code> can reference
17 <code class="varname">a</code>. But once <code class="function">f</code> returns,
18 the variable <code class="varname">a</code> goes out of scope.
19 For example, the following code will print out 5.
20 The function <code class="function">g</code> cannot be called on the
21 top level (outside <code class="function">f</code> as <code class="varname">a</code>
22 will not be defined).
23 </p><pre lang="en" class="programlisting">function f() = (a:=5; g());
24 function g() = print(a);
25 f();
26 </pre><p lang="en">
27 </p><p lang="en">
28 If you define a variable inside a function it will override
29 any variables defined in calling functions. For example,
30 we modify the above code and write:
31 </p><pre lang="en" class="programlisting">function f() = (a:=5; g());
32 function g() = print(a);
33 a:=10;
34 f();
35 </pre><p lang="en">
36 This code will still print out 5. But if you call
37 <code class="function">g</code> outside of <code class="function">f</code> then
38 you will get a printout of 10. Note that
39 setting <code class="varname">a</code>
40 to 5 inside <code class="function">f</code> does not change
41 the value of <code class="varname">a</code> at the top (global) level,
42 so if you now check the value of <code class="varname">a</code> it will
43 still be 10.
44 </p><p lang="en">
45 Function arguments are exactly like variables defined inside
46 the function, except that they are initialized with the value
47 that was passed to the function. Other than this point, they are
48 treated just like all other variables defined inside the
49 function.
50 </p><p lang="en">
51 Functions are treated exactly like variables. Hence you can
52 locally redefine functions. Normally (on the top level) you
53 cannot redefine protected variables and functions. But locally
54 you can do this. Consider the following session:
55 </p><pre lang="en" class="screen"><code class="prompt">genius&gt; </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
56 = (`(x)=(sin(x)^2))
57 <code class="prompt">genius&gt; </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
58 = (`(x)=(sin(x)^2))
59 <code class="prompt">genius&gt; </code><strong class="userinput"><code>function g(x) = ((function sin(x)=x^10);f(x))</code></strong>
60 = (`(x)=((sin:=(`(x)=(x^10)));f(x)))
61 <code class="prompt">genius&gt; </code><strong class="userinput"><code>g(10)</code></strong>
62 = 1e20
63 </pre><p lang="en">
64 </p><p lang="en">
65 Functions and variables defined at the top level are
66 considered global. They are visible from anywhere. As we
67 said the following function <code class="function">f</code>
68 will not change the value of <code class="varname">a</code> to 5.
69 </p><pre lang="en" class="programlisting">a=6;
70 function f() = (a:=5);
71 f();
72 </pre><p lang="en">
73 Sometimes, however, it is necessary to set
74 a global variable from inside a function. When this behavior is needed,
75 use the
76 <a class="link" href="ch11s02.html#gel-function-set"><code class="function">set</code></a> function. Passing a string or a quoted identifier to
77 this function sets the variable globally (on the top level).
78 For example, to set
79 <code class="varname">a</code> to the value 3 you could call:
80 </p><pre lang="en" class="programlisting">set(`a,3)
81 </pre><p lang="en">
82 or:
83 </p><pre lang="en" class="programlisting">set("a",3)
84 </pre><p lang="en">
85 </p><p lang="en">
86 The <code class="function">set</code> function always sets the toplevel
87 global. There is no way to set a local variable in some function
88 from a subroutine. If this is required, must use passing by
89 reference.
90 </p><p lang="en">
91 See also the
92 <a class="link" href="ch11s02.html#gel-function-SetElement"><code class="function">SetElement</code></a> and
93 <a class="link" href="ch11s02.html#gel-function-SetVElement"><code class="function">SetVElement</code></a> functions.
94 </p><p lang="en">
95 So to recap in a more technical language: Genius operates with
96 different numbered contexts. The top level is the context 0
97 (zero). Whenever a function is entered, the context is raised,
98 and when the function returns the context is lowered. A function
99 or a variable is always visible from all higher numbered contexts.
100 When a variable was defined in a lower numbered context, then
101 setting this variable has the effect of creating a new local
102 variable in the current context number and this variable
103 will now be visible from all higher numbered contexts.
104 </p><p lang="en">
105 There are also true local variables that are not seen from
106 anywhere but the current context. Also when returning functions
107 by value it may reference variables not visible from higher context
108 and this may be a problem. See the sections
109 <a class="link" href="ch07s04.html" title="True Local Variables">True
110 Local Variables</a> and
111 <a class="link" href="ch07s03.html" title="Returning Functions">Returning
112 Functions</a>.
113 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s04.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch06s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Comparison Operators </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Parameter variables</td></tr></table></div></body></html>