<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Global Variables (GNU Octave (version 5.1.0))</title> <meta name="description" content="Global Variables (GNU Octave (version 5.1.0))"> <meta name="keywords" content="Global Variables (GNU Octave (version 5.1.0))"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="makeinfo"> <link href="index.html#Top" rel="start" title="Top"> <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index"> <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents"> <link href="Variables.html#Variables" rel="up" title="Variables"> <link href="Persistent-Variables.html#Persistent-Variables" rel="next" title="Persistent Variables"> <link href="Variables.html#Variables" rel="prev" title="Variables"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} blockquote.indentedblock {margin-right: 0em} blockquote.smallindentedblock {margin-right: 0em; font-size: smaller} blockquote.smallquotation {font-size: smaller} div.display {margin-left: 3.2em} div.example {margin-left: 3.2em} div.lisp {margin-left: 3.2em} div.smalldisplay {margin-left: 3.2em} div.smallexample {margin-left: 3.2em} div.smalllisp {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} pre.smalldisplay {font-family: inherit; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: inherit; font-size: smaller} pre.smalllisp {font-size: smaller} 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"> <a name="Global-Variables"></a> <div class="header"> <p> Next: <a href="Persistent-Variables.html#Persistent-Variables" accesskey="n" rel="next">Persistent Variables</a>, Up: <a href="Variables.html#Variables" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> <hr> <a name="Global-Variables-1"></a> <h3 class="section">7.1 Global Variables</h3> <a name="index-global-variables"></a> <a name="index-global-statement"></a> <a name="index-variables_002c-global"></a> <p>A variable that has been declared <em>global</em> may be accessed from within a function body without having to pass it as a formal parameter. </p> <p>A variable may be declared global 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>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 it. 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. 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 not 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> <a name="XREFisglobal"></a><dl> <dt><a name="index-isglobal"></a><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#Persistent-Variables" accesskey="n" rel="next">Persistent Variables</a>, Up: <a href="Variables.html#Variables" 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#Concept-Index" title="Index" rel="index">Index</a>]</p> </div> </body> </html>