| 1 | <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"> |
| 2 | Like most programming languages, GEL has different types |
| 3 | of variables. Normally when a variable is defined in a function, |
| 4 | it is visible from that function and from all functions that are |
| 5 | called (all higher contexts). For example, suppose a function |
| 6 | <code class="function">f</code> defines a variable <code class="varname">a</code> |
| 7 | and then calls function <code class="function">g</code>. Then |
| 8 | function <code class="function">g</code> can reference |
| 9 | <code class="varname">a</code>. But once <code class="function">f</code> returns, |
| 10 | the variable <code class="varname">a</code> goes out of scope. |
| 11 | For example, the following code will print out 5. |
| 12 | The function <code class="function">g</code> cannot be called on the |
| 13 | top level (outside <code class="function">f</code> as <code class="varname">a</code> |
| 14 | will not be defined). |
| 15 | </p><pre lang="en" class="programlisting">function f() = (a:=5; g()); |
| 16 | function g() = print(a); |
| 17 | f(); |
| 18 | </pre><p lang="en"> |
| 19 | </p><p lang="en"> |
| 20 | If you define a variable inside a function it will override |
| 21 | any variables defined in calling functions. For example, |
| 22 | we modify the above code and write: |
| 23 | </p><pre lang="en" class="programlisting">function f() = (a:=5; g()); |
| 24 | function g() = print(a); |
| 25 | a:=10; |
| 26 | f(); |
| 27 | </pre><p lang="en"> |
| 28 | This code will still print out 5. But if you call |
| 29 | <code class="function">g</code> outside of <code class="function">f</code> then |
| 30 | you will get a printout of 10. Note that |
| 31 | setting <code class="varname">a</code> |
| 32 | to 5 inside <code class="function">f</code> does not change |
| 33 | the value of <code class="varname">a</code> at the top (global) level, |
| 34 | so if you now check the value of <code class="varname">a</code> it will |
| 35 | still be 10. |
| 36 | </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"> |
| 37 | Functions are treated exactly like variables. Hence you can |
| 38 | locally redefine functions. Normally (on the top level) you |
| 39 | cannot redefine protected variables and functions. But locally |
| 40 | you can do this. Consider the following session: |
| 41 | </p><pre lang="en" class="screen"><code class="prompt">genius> </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong> |
| 42 | = (`(x)=(sin(x)^2)) |
| 43 | <code class="prompt">genius> </code><strong class="userinput"><code>function f(x) = sin(x)^2</code></strong> |
| 44 | = (`(x)=(sin(x)^2)) |
| 45 | <code class="prompt">genius> </code><strong class="userinput"><code>function g(x) = ((function sin(x)=x^10);f(x))</code></strong> |
| 46 | = (`(x)=((sin:=(`(x)=(x^10)));f(x))) |
| 47 | <code class="prompt">genius> </code><strong class="userinput"><code>g(10)</code></strong> |
| 48 | = 1e20 |
| 49 | </pre><p lang="en"> |
| 50 | </p><p lang="en"> |
| 51 | Functions and variables defined at the top level are |
| 52 | considered global. They are visible from anywhere. As we |
| 53 | said the following function <code class="function">f</code> |
| 54 | will not change the value of <code class="varname">a</code> to 5. |
| 55 | </p><pre lang="en" class="programlisting">a=6; |
| 56 | function f() = (a:=5); |
| 57 | f(); |
| 58 | </pre><p lang="en"> |
| 59 | Sometimes, however, it is necessary to set |
| 60 | a global variable from inside a function. When this behavior is needed, |
| 61 | use the |
| 62 | <a class="link" href="ch11s02.html#gel-function-set"><code class="function">set</code></a> function. Passing a string or a quoted identifier to |
| 63 | this function sets the variable globally (on the top level). |
| 64 | For example, to set |
| 65 | <code class="varname">a</code> to the value 3 you could call: |
| 66 | </p><pre lang="en" class="programlisting">set(`a,3) |
| 67 | </pre><p lang="en"> |
| 68 | or: |
| 69 | </p><pre lang="en" class="programlisting">set("a",3) |
| 70 | </pre><p lang="en"> |
| 71 | </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> |