help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Returned value from function as a global variable


From: John W. Eaton
Subject: Returned value from function as a global variable
Date: Wed, 24 Sep 2008 13:34:00 -0400

On 25-Sep-2008, LUK ShunTim wrote:

| I had some matlab code which uses global variables to allow common
| access to returned values from functions. It appears that octave doesn't
| allow such behaviour. Here's a short function which produces an error in
| octave.
| 
| function [v]=t_global_f(x)
|     global v;
|     v = sin(x);
| endfunction
| 
| octave:1> x=0:0.1:pi;
| octave:2> v=t_global_f(x);
| error: can't make function parameter `v' global
| error: evaluating global command near line 6, column 1
| error: called from `t_global_f' in file
| `/home/0/00work/octave/00snippets/t_global_f.m'
| error: evaluating assignment expression near line 2, column 2
| 
| Is there any plan to implement the same behaviour in octave?

Not for any 3.0.x release.

I thought this already worked with the current development sources,
but the following small patch was needed to make it work properly.

For now, you should be able to work around the problem by writing your
function like this:

  function r = t_global_f (x)
    global v;
    r = v = sin (x);
  endfunction

jwe


# HG changeset patch
# User John W. Eaton <address@hidden>
# Date 1222277464 14400
# Node ID 54b41376e381691eaa00b93efdf0a937b72bb651
# Parent  31d7885d98690ca9823082227870063bfb43514c
ov-usr-fcn.cc (octave_user_function::do_multi_index_op): add 
symbol_table::clear_variables cleanup function to the unwind_protect stack 
after the parameter list cleanup functions

diff --git a/src/ChangeLog b/src/ChangeLog
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,4 +1,8 @@
 2008-09-24  John W. Eaton  <address@hidden>
+
+       * ov-usr-fcn.cc (octave_user_function::do_multi_index_op):
+       Add symbol_table::clear_variables cleanup function to the
+       unwind_protect stack after the parameter list cleanup functions.
 
        * load-path.cc (load_path::do_initialize): Check for OCTAVE_PATH
        in the environment, not OCTAVE_LOADPATH.
diff --git a/src/ov-usr-fcn.cc b/src/ov-usr-fcn.cc
--- a/src/ov-usr-fcn.cc
+++ b/src/ov-usr-fcn.cc
@@ -375,11 +375,6 @@
 
       unwind_protect::add (symbol_table::pop_context);
     }
-  else
-    {
-      // Force symbols to be undefined again when this function exits.
-      unwind_protect::add (symbol_table::clear_variables);
-    }
 
   // Save and restore args passed for recursive calls.
 
@@ -410,6 +405,21 @@
   // variables that are also named values returned by this function.
 
   unwind_protect::add (clear_param_list, ret_list);
+
+  if (call_depth == 0)
+    {
+      // Force symbols to be undefined again when this function
+      // exits.
+      //
+      // This cleanup function is added to the unwind_protect stack
+      // after the calls to clear the parameter lists so that local
+      // variables will be cleared before the parameter lists are
+      // cleared.  That way, any function parameters that have been
+      // declared global will be unmarked as global before they are
+      // undefined by the clear_param_list cleanup function.
+
+      unwind_protect::add (symbol_table::clear_variables);
+    }
 
   // The following code is in a separate scope to avoid warnings from
   // G++ about `goto abort' crossing the initialization of some

reply via email to

[Prev in Thread] Current Thread [Next in Thread]