12.07.2015 Views

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

GNU Octave - Local Sector 7 web page

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

56 <strong>GNU</strong> <strong>Octave</strong>global xfunction f ()x = 1;endfunctionf ()does not set the value of the global variable x to 1. In order to change the value of theglobal variable x, you must also declare it to be global within the function body, like thisfunction f ()global x;x = 1;endfunctionPassing a global variable in a function parameter list will make a local copy and notmodify the global value. For example, given the functionfunction f (x)x = 0endfunctionand the definition of x as a global variable at the top level,global x = 13the expressionf (x)will display the value of x from inside the function as 0, but the value of x at the top levelremains unchanged, because the function works with a copy of its argument.isglobal (name)Built-in FunctionReturn 1 if name is globally visible. Otherwise, return 0. For example,global xisglobal ("x")⇒ 19.2 Persistent VariablesA variable that has been declared persistent within a function will retain its contents inmemory between subsequent calls to the same function. The difference between persistentvariables and global variables is that persistent variables are local in scope to a particularfunction and are not visible elsewhere.A variable may be declared persistent using a persistent declaration statement. Thefollowing statements are all persistent declarations.persistent apersistent a bpersistent c = 2persistent d = 3 e f = 5The behavior of persistent variables is equivalent to the behavior of static variables in C.The command static in octave is also recognized and is equivalent to persistent. Unlikeglobal variables, every initialization statement will re-initialize the variable. For example,after executing the following code

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!