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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Global Variables (GNU Octave (version 6.2.0))</title>
<meta name="description" content="Global Variables (GNU Octave (version 6.2.0))">
<meta name="keywords" content="Global Variables (GNU Octave (version 6.2.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Variables.html" rel="up" title="Variables">
<link href="Persistent-Variables.html" rel="next" title="Persistent Variables">
<link href="Variables.html" rel="prev" title="Variables">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<span id="Global-Variables"></span><div class="header">
<p>
Next: <a href="Persistent-Variables.html" accesskey="n" rel="next">Persistent Variables</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<span id="Global-Variables-1"></span><h3 class="section">7.1 Global Variables</h3>
<span id="index-global-variables"></span>
<span id="index-global-statement"></span>
<span id="index-variables_002c-global"></span>
<p>A <em>global</em> variable is one that may be accessed anywhere within Octave.
This is in contrast to a local variable which can only be accessed outside
of its current context if it is passed explicitly, such as by including it as a
parameter when calling a function
(<code>fcn (<var>local_var1</var>, <var>local_var2</var>)</code>).
</p>
<p>A variable is declared global by using a <code>global</code> declaration statement.
The following statements are all global declarations.
</p>
<div class="example">
<pre class="example">global a
global a b
global c = 2
global d = 3 e f = 5
</pre></div>
<p>Note that the <code>global</code> qualifier extends only to the next end-of-statement
indicator which could be a comma (‘<samp>,</samp>’), semicolon (‘<samp>;</samp>’), or newline
(‘<samp>'\n'</samp>’). For example, the following code declares one global variable,
<var>a</var>, and one local variable <var>b</var> to which the value 1 is assigned.
</p>
<div class="example">
<pre class="example">global a, b = 1
</pre></div>
<p>A global variable may only be initialized once in a <code>global</code> statement.
For example, after executing the following code
</p>
<div class="example">
<pre class="example">global gvar = 1
global gvar = 2
</pre></div>
<p>the value of the global variable <code>gvar</code> is 1, not 2. Issuing a
‘<samp>clear gvar</samp>’ command does not change the above behavior, but
‘<samp>clear all</samp>’ does.
</p>
<p>It is necessary declare a variable as global within a function body in order to
access the one universal variable. For example,
</p>
<div class="example">
<pre class="example">global x
function f ()
x = 1;
endfunction
f ()
</pre></div>
<p>does <em>not</em> set the value of the global variable <code>x</code> to 1. Instead,
a local variable, with name <code>x</code>, is created and assigned the value of 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
</p>
<div class="example">
<pre class="example">function f ()
global x;
x = 1;
endfunction
</pre></div>
<p>Passing a global variable in a function parameter list will make a local copy
and <em>not</em> modify the global value. For example, given the function
</p>
<div class="example">
<pre class="example">function f (x)
x = 0
endfunction
</pre></div>
<p>and the definition of <code>x</code> as a global variable at the top level,
</p>
<div class="example">
<pre class="example">global x = 13
</pre></div>
<p>the expression
</p>
<div class="example">
<pre class="example">f (x)
</pre></div>
<p>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.
</p>
<p>Programming Note: While global variables occasionally are the right solution to
a coding problem, modern best practice discourages their use. Code which
relies on global variables may behave unpredictably between different users
and can be difficult to debug. This is because global variables can introduce
systemic changes so that localizing a bug to a particular function, or to a
particular loop within a function, becomes difficult.
</p>
<span id="XREFisglobal"></span><dl>
<dt id="index-isglobal">: <em></em> <strong>isglobal</strong> <em>(<var>name</var>)</em></dt>
<dd><p>Return true if <var>name</var> is a globally visible variable.
</p>
<p>For example:
</p>
<div class="example">
<pre class="example">global x
isglobal ("x")
⇒ 1
</pre></div>
<p><strong>See also:</strong> <a href="Variables.html#XREFisvarname">isvarname</a>, <a href="Status-of-Variables.html#XREFexist">exist</a>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Persistent-Variables.html" accesskey="n" rel="next">Persistent Variables</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|