File: ch06s05.html

package info (click to toggle)
genius 1.0.27-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 25,308 kB
  • sloc: ansic: 75,620; xml: 71,565; sh: 4,445; makefile: 1,927; lex: 523; yacc: 298; perl: 54
file content (113 lines) | stat: -rw-r--r-- 7,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<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="Genius Manual"><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>
	  GEL is a
	  <a class="ulink" href="https://en.wikipedia.org/wiki/Scope_%28programming%29" target="_top">
	  dynamically scoped language</a>.  We will explain what this
	  means below.  That is, normal variables and functions are dynamically
	  scoped.  The exception are 
	  <a class="link" href="ch06s06.html" title="Parameter variables">parameter variables</a>,
	  which are always global.
	</p><p>
	  Like most programming languages, GEL has different types
	  of variables.  Normally when a variable is defined in a function,
	  it is visible from that function and from all functions that are
	  called (all higher contexts).  For example, suppose a function
	  <code class="function">f</code> defines a variable <code class="varname">a</code>
	  and then calls function <code class="function">g</code>.  Then
	  function <code class="function">g</code> can reference
	  <code class="varname">a</code>.  But once <code class="function">f</code> returns,
	  the variable <code class="varname">a</code> goes out of scope.
	  For example, the following code will print out 5.
	  The function <code class="function">g</code> cannot be called on the
	  top level (outside <code class="function">f</code> as <code class="varname">a</code>
	  will not be defined).
</p><pre class="programlisting">function f() = (a:=5; g());
function g() = print(a);
f();
</pre><p>
        </p><p>
	  If you define a variable inside a function it will override
	  any variables defined in calling functions.  For example,
	  we modify the above code and write:
</p><pre class="programlisting">function f() = (a:=5; g());
function g() = print(a);
a:=10;
f();
</pre><p>
	  This code will still print out 5.  But if you call
	  <code class="function">g</code> outside of <code class="function">f</code> then
	  you will get a printout of 10.  Note that
	  setting <code class="varname">a</code>
	  to 5 inside <code class="function">f</code> does not change
	  the value of <code class="varname">a</code> at the top (global) level,
	  so if you now check the value of <code class="varname">a</code> it will
	  still be 10.
        </p><p>
	  Function arguments are exactly like variables defined inside
	  the function, except that they are initialized with the value
	  that was passed to the function.  Other than this point, they are
	  treated just like all other variables defined inside the
	  function.
	</p><p>
	  Functions are treated exactly like variables.  Hence you can
	  locally redefine functions.  Normally (on the top level) you
	  cannot redefine protected variables and functions.  But locally
	  you can do this.  Consider the following session:
</p><pre class="screen"><code class="prompt">genius&gt; </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
= (`(x)=(sin(x)^2))
<code class="prompt">genius&gt; </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
= (`(x)=(sin(x)^2))
<code class="prompt">genius&gt; </code><strong class="userinput"><code>function g(x) = ((function sin(x)=x^10);f(x))</code></strong>
= (`(x)=((sin:=(`(x)=(x^10)));f(x)))
<code class="prompt">genius&gt; </code><strong class="userinput"><code>g(10)</code></strong>
= 1e20
</pre><p>
	</p><p>
	  Functions and variables defined at the top level are
	  considered global.  They are visible from anywhere.  As we
	  said the following function <code class="function">f</code>
	  will not change the value of <code class="varname">a</code> to 5.
</p><pre class="programlisting">a=6;
function f() = (a:=5);
f();
</pre><p>
	  Sometimes, however, it is necessary to set
a global variable from inside a function.  When this behavior is needed,
use the
<a class="link" href="ch11s02.html#gel-function-set"><code class="function">set</code></a> function. Passing a string or a quoted identifier to
this function sets the variable globally (on the top level).
For example, to set
<code class="varname">a</code> to the value 3 you could call:
</p><pre class="programlisting">set(`a,3)
</pre><p>
or:
</p><pre class="programlisting">set("a",3)
</pre><p>
        </p><p>
	  The <code class="function">set</code> function always sets the toplevel
	  global.  There is no way to set a local variable in some function
	  from a subroutine.  If this is required, must use passing by
	  reference.
        </p><p>
		See also the
		<a class="link" href="ch11s02.html#gel-function-SetElement"><code class="function">SetElement</code></a> and
		<a class="link" href="ch11s02.html#gel-function-SetVElement"><code class="function">SetVElement</code></a> functions.
	</p><p>
	  So to recap in a more technical language:  Genius operates with
	  different numbered contexts.  The top level is the context 0
	  (zero).  Whenever a function is entered, the context is raised,
	  and when the function returns the context is lowered.  A function
	  or a variable is always visible from all higher numbered contexts.
	  When a variable was defined in a lower numbered context, then
	  setting this variable has the effect of creating a new local
	  variable in the current context number and this variable
	  will now be visible from all higher numbered contexts.
	</p><p>
	  There are also true local variables that are not seen from
	  anywhere but the current context.  Also when returning functions
	  by value it may reference variables not visible from higher context
	  and this may be a problem.  See the sections
	  <a class="link" href="ch07s04.html" title="True Local Variables">True
	  Local Variables</a> and
	  <a class="link" href="ch07s03.html" title="Returning Functions">Returning
	  Functions</a>.
	</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>