octave-maintainers
[Top][All Lists]
Advanced

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

Re: Code after sub-functions?


From: David Bateman
Subject: Re: Code after sub-functions?
Date: Fri, 24 Aug 2007 16:02:55 +0200
User-agent: Thunderbird 1.5.0.7 (X11/20060921)

Michael Goffioul wrote:
> Hi,
>
> Is it legal to have code after subfunctions declaration? This works
> under Matlab:
>
> --
> function f1
>   disp('hello from f1');
>
> function sub1
>   disp('hello from sub1');
> end
>
> function sub2
>   disp('hello from sub2');
> end
>
>   disp('hello from f1 gain');
> end
>
> function sub3
>   disp('hello from sub3');
> end
> --
>
> Matlab prints:
>
> hello from test1
> hello from test1 again
>
> While octave only prints:
> hello from test1
>
> In other words, it doesn't execute the code after the subfunction 
> declarations.
>
> Note: this kind of code is generated by the GUI editor of Matlab, so it *may*
> happen.
>
> Michael.
>
>   
In the above "sub1" and "sub2" are nested functions while "sub3" is a
sub-function. Matlab introduced nested functions in v7, and Octave
doesn't support them. Note the scoping rules are different for nested
functions, as the nested function (and any nested function of it), can
see the symbol table of its parent, so something like

function y = f (x)
    i = x +1;
    function y = sub (x)
        y = x + i;
    end
    y = i + sub(2);
end

is legal. Octave assumes that the next occurrence of the keyword
"function" defines the start of a new sub-function, which terminates the
definition of the parent function (or previous sub-function). Thus

function y = f(x)
   y = sub1(x);
function y = sub1 (x)
   y = sub2 (x);
function y = sub2 (x)
   y = x +1;

is correct. This syntax is legal as Matlab doesn't require functions to
be terminates with an "end" keyword, except when nested functions are
used for the parent and nested functions. Octave only duplicates the
previous syntax for sub-functions. So I'm afraid you have two issues
here. Firstly the unexecuted code, and seconding do the nested functions
make use of variables in the scope of the parent?

D.

-- 
David Bateman                                address@hidden
Motorola Labs - Paris                        +33 1 69 35 48 04 (Ph) 
Parc Les Algorithmes, Commune de St Aubin    +33 6 72 01 06 33 (Mob) 
91193 Gif-Sur-Yvette FRANCE                  +33 1 69 35 77 01 (Fax) 

The information contained in this communication has been classified as: 

[x] General Business Information 
[ ] Motorola Internal Use Only 
[ ] Motorola Confidential Proprietary



reply via email to

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