help-octave
[Top][All Lists]
Advanced

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

Scripts within functions


From: John W. Eaton
Subject: Scripts within functions
Date: Mon, 10 Oct 2011 10:52:21 -0400

On 10-Oct-2011, John W. Eaton wrote:

| On 10-Oct-2011, José Vallet wrote:
| 
| | Is it possible to define and use scripts within functions?
| | 
| | I'd like to move some blocks of code (scripts) to the end of the file 
| | and then call the scripts in different parts of the function. The 
| | purpose is simply for cleanness of the code.
| | 
| | I know that I can define functions inside functions. That is not what I 
| | want, as in this particular case the list of input and output arguments 
| | would get too complicated.
| 
| Currently in Octave, the script parts would have to go in separate
| files and you can't pass any arguments to them.

Maybe I should clarify this a bit with an example:

In file foo.m:

  function foo
    ## long and complex function.
    disp ('begin foo')
    x = 1
    foo_script;
    ## more complex stuff.
    x
    y
    x = 2
    foo_script;
    x
    y
    disp ('end foo')
  endfunction

In file foo_script.m:

   ## some code to be executed at various points in foo.m
   disp ('begin foo_script')
   x
   x = 3
   y = -1
   disp ('end foo_script')

You can't pass parameters to foo_script with a call like
foo_script (arg1, arg2), but since the code in foo_script is executed
in the context of the function foo, all variables are shared, so
foo_script has access to all the variables used in foo (and can
introdoce new variables for foo as well).

jwe




reply via email to

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