help-octave
[Top][All Lists]
Advanced

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

embedded functions


From: John W. Eaton
Subject: embedded functions
Date: Fri, 24 Mar 2006 13:39:21 -0500

On 24-Mar-2006, Gorazd Brumen wrote:

| hi all,
| 
| does octave allows this in an function file:
| 
| function y = foo (x)
| 
|       ...blabla....
|       r = foo1( x)
| 
|       function z = foo1 (g)
|               ...blabla2...
|       endfunction
| 
| 
| endfunction
| 
| such that foo1 is visible only to foo1?

No.

Octave does provide subfunctions, but not nested functions.

FWIW, I think the addition of nested functions to Matlab without the
addition of away to specify local variables was a bad idea.  The
problem is that there is no other place in the language where the
scoping rules are similar, and without a way to declare local
variables, you are just asking for trouble.  It may seem nice for
nested functions to inherit the workspace(s) of the enclosing
function(s) but when the functions become large and have the functions
are small and there are only a few variables, but when the functions
get to be larger with many variables, it seems to me that it is just
too easy to introduce bugs.  For example, if you have

  function parent_fcn ()
    %% many variables defined and used here, including:
    some_variable = 1;

    %% now the call to nested function, which can see and modify all
    %% the variables in parent_fcn.
    nested_fcn ();

    %% more calculations with variables here, including
    a_calculation_with (some_variable);

    function nested_fcn ()
      %% Imagine this function initially does not touch any of the
      %% variables in parent_fcn.  But sometime later, it is modified
      %% with the addition of some_variable that happens to have the
      %% same names as some_variable in use in parent_fcn.  The function
      %% fails mysteriously.  It is hard to see why.  To avoid
      %% problems like this, you must know the entire list of variables
      %% used in parent_fcn before you can make any changes to
      %% nested_fcn.
    end

  end


jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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