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
|
<html lang="en">
<head>
<title>Global Variables - Untitled</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="Untitled">
<meta name="generator" content="makeinfo 4.11">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Variables.html#Variables" title="Variables">
<link rel="next" href="Persistent-Variables.html#Persistent-Variables" title="Persistent Variables">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
</head>
<body>
<div class="node">
<p>
<a name="Global-Variables"></a>
Next: <a rel="next" accesskey="n" href="Persistent-Variables.html#Persistent-Variables">Persistent Variables</a>,
Up: <a rel="up" accesskey="u" href="Variables.html#Variables">Variables</a>
<hr>
</div>
<h3 class="section">7.1 Global Variables</h3>
<p><a name="index-global-variables-423"></a><a name="index-g_t_0040code_007bglobal_007d-statement-424"></a><a name="index-variables_002c-global-425"></a>
A variable that has been declared <dfn>global</dfn> may be accessed from
within a function body without having to pass it as a formal parameter.
<p>A variable may be declared global using a <code>global</code> declaration
statement. The following statements are all global declarations.
<pre class="example"> global a
global a b
global c = 2
global d = 3 e f = 5
</pre>
<p>A global variable may only be initialized once in a <code>global</code>
statement. For example, after executing the following code
<pre class="example"> global gvar = 1
global gvar = 2
</pre>
<p class="noindent">the value of the global variable <code>gvar</code> is 1, not 2. Issuing a
‘<samp><span class="samp">clear gvar</span></samp>’ command does not change the above behavior, but
‘<samp><span class="samp">clear all</span></samp>’ does.
<p>It is necessary declare a variable as global within a function body in
order to access it. For example,
<pre class="example"> global x
function f ()
x = 1;
endfunction
f ()
</pre>
<p class="noindent">does <em>not</em> set the value of the global variable <code>x</code> to 1. In
order to change the value of the global variable <code>x</code>, you must also
declare it to be global within the function body, like this
<pre class="example"> function f ()
global x;
x = 1;
endfunction
</pre>
<p>Passing a global variable in a function parameter list will
make a local copy and not modify the global value. For example, given
the function
<pre class="example"> function f (x)
x = 0
endfunction
</pre>
<p class="noindent">and the definition of <code>x</code> as a global variable at the top level,
<pre class="example"> global x = 13
</pre>
<p class="noindent">the expression
<pre class="example"> f (x)
</pre>
<p class="noindent">will display the value of <code>x</code> from inside the function as 0,
but the value of <code>x</code> at the top level remains unchanged, because
the function works with a <em>copy</em> of its argument.
<!-- variables.cc -->
<p><a name="doc_002disglobal"></a>
<div class="defun">
— Built-in Function: <b>isglobal</b> (<var>name</var>)<var><a name="index-isglobal-426"></a></var><br>
<blockquote><p>Return 1 if <var>name</var> is globally visible. Otherwise, return 0. For
example,
<pre class="example"> global x
isglobal ("x")
1
</pre>
</blockquote></div>
</body></html>
|