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
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Globální proměnné a působnost proměnných</title><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot"><link rel="home" href="index.html" title="Příručka k aplikaci Genius"><link rel="up" href="ch06.html" title="Chapter 6. Programování s jazykem GEL"><link rel="prev" href="ch06s04.html" title="Porovnávací operátory"><link rel="next" href="ch06s06.html" title="Proměnné parametrů"></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">Globální proměnné a působnost proměnných</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s04.html">Prev</a> </td><th width="60%" align="center">Chapter 6. Programování s jazykem 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>Globální proměnné a působnost proměnných</h2></div></div></div><p>GEL je <a class="ulink" href="https://en.wikipedia.org/wiki/Scope_%28programming%29" target="_top">jazyk s dynamickým rozsahem platnosti</a>. Co to znamená hned vysvětlíme. Je to to, že normální proměnné a funkce mají dynamicky vymezenou platnost. Výjimkou jsou <a class="link" href="ch06s06.html" title="Proměnné parametrů">proměnné parametrů</a>, kterou jsou vždy globální.</p><p lang="en">
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 lang="en" class="programlisting">function f() = (a:=5; g());
function g() = print(a);
f();
</pre><p lang="en">
</p><p lang="en">
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 lang="en" class="programlisting">function f() = (a:=5; g());
function g() = print(a);
a:=10;
f();
</pre><p lang="en">
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>Argumenty funkce jsou úplně stejné jako proměnné definované uvnitř funkce vyjma toho, že jsou inicializovány na hodnotu, která je funkci předána. Kromě této jediné věci se s nimi zachází úplně stejně, jako se všemi ostatními proměnnými definovanými uvnitř funkce.</p><p lang="en">
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 lang="en" class="screen"><code class="prompt">genius> </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
= (`(x)=(sin(x)^2))
<code class="prompt">genius> </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong>
= (`(x)=(sin(x)^2))
<code class="prompt">genius> </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> </code><strong class="userinput"><code>g(10)</code></strong>
= 1e20
</pre><p lang="en">
</p><p lang="en">
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 lang="en" class="programlisting">a=6;
function f() = (a:=5);
f();
</pre><p lang="en">
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 lang="en" class="programlisting">set(`a,3)
</pre><p lang="en">
or:
</p><pre lang="en" class="programlisting">set("a",3)
</pre><p lang="en">
</p><p>Funkce <code class="function">set</code> nastavuje vždy globální proměnné v nejvyšší úrovni. Neexistuje žádný způsob, jak nastavit lokální proměnnou v nějaké funkce z podřízené funkce. Pokud něco takového potřebujete, musíte jedině použít předání reference (odkazu).</p><p>Viz také funkce <a class="link" href="ch11s02.html#gel-function-SetElement"><code class="function">SetElement</code></a> a <a class="link" href="ch11s02.html#gel-function-SetVElement"><code class="function">SetVElement</code></a>.</p><p>Takže sesumírováno do technického jazyka: Genius pracuje s různými očíslovanými kontexty. Nejvyšší úroveň je kontext 0 (nula). Kdykoliv se vstoupí do funkce, je kontext zvýšen a když se funkce opouští, je kontext snížen. Funkce nebo proměnná je vždy viditelná ze všech kontextů, které mají vyšší číslo. Když byla proměnná definována v kontextu s nižším číslem, má nastavení této proměnné vliv na vytváření nové lokální proměnné v aktuálním čísle kontextu a tato proměnná bude nyní viditelná ze všech kontextů s vyšším číslem.</p><p>Existují i skutečně lokální proměnné, které nejsou vidět nikde jinde, než v aktuálním kontextu. Rovněž při vracení funkcí hodnotou je možné odkazovat na proměnnou, která není viditelná z vyššího kontextu a to může být problém. Viz oddíl <a class="link" href="ch07s04.html" title="Skutečně lokální proměnné">Skutečně lokální proměnné</a> a <a class="link" href="ch07s03.html" title="Vracení funkcí">Vracení funkcí</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">Porovnávací operátory </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Proměnné parametrů</td></tr></table></div></body></html>
|