Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 285

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Global Variables - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<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">
<a name="Global-Variables"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Persistent-Variables.html#Persistent-Variables">Persistent Variables</a>,
Up:&nbsp;<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-484"></a><a name="index-g_t_0040code_007bglobal_007d-statement-485"></a><a name="index-variables_002c-global-486"></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
&lsquo;<samp><span class="samp">clear gvar</span></samp>&rsquo; command does not change the above behavior, but
&lsquo;<samp><span class="samp">clear all</span></samp>&rsquo; 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.

<!-- isglobal src/variables.cc -->
   <p><a name="doc_002disglobal"></a>

<div class="defun">
&mdash; Built-in Function:  <b>isglobal</b> (<var>name</var>)<var><a name="index-isglobal-487"></a></var><br>
<blockquote><p>Return true if <var>name</var> is a globally visible variable. 
For example:

     <pre class="example">          global x
          isglobal ("x")
               &rArr; 1
</pre>
        <!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
     <!-- A simple blank line produces the correct behavior. -->
     <!-- @sp 1 -->
     <p class="noindent"><strong>See also:</strong> <a href="doc_002disvarname.html#doc_002disvarname">isvarname</a>, <a href="doc_002dexist.html#doc_002dexist">exist</a>. 
</p></blockquote></div>

   </body></html>