#### A.1.7 Accessing Global Variables in Oct-Files

Global variables allow variables in the global scope to be accessed. Global variables can easily be accessed with oct-files using the support functions `get_global_value` and `set_global_value`. `get_global_value` takes two arguments, the first is a string representing the variable name to obtain. The second argument is a boolean argument specifying what to do in the case that no global variable of the desired name is found. An example of the use of these two functions is

         /*

         Copyright (C) 2006, 2007 John W. Eaton

         This file is part of Octave.

         Octave is free software; you can redistribute it and/or
         modify it under the terms of the GNU General Public License
         as published by the Free Software Foundation; either
         version 3  of the License, or (at your option) any later
         version.

         Octave is distributed in the hope that it will be useful,
         but WITHOUT ANY WARRANTY; without even the implied warranty
         of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
         See the GNU General Public License for more details.

         You should have received a copy of the GNU General Public
         License along with Octave; see the file COPYING.  If not,
         see <http://www.gnu.org/licenses/>.

         */

         #include <octave/oct.h>

         DEFUN_DLD (globaldemo, args, , "Global demo.")
         {
           int nargin = args.length ();
           octave_value retval;

           if (nargin != 1)
             print_usage ();
           else
             {
               std::string s = args(0).string_value ();
               if (! error_state)
                 {
                   octave_value tmp = get_global_value (s, true);
                   if (tmp.is_defined ())
                     retval = tmp;
                   else
                     retval = "Global variable not found";

                   set_global_value ("a", 42.0);
                 }
             }
           return retval;
         }

An example of its use is

         global a b
         b = 10;
         globaldemo ("b")
          10
         globaldemo ("c")
          "Global variable not found"
         num2str (a)
          42