[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Expected behavior for variables which shadow function names
From: |
Thorsten Liebig |
Subject: |
Re: Expected behavior for variables which shadow function names |
Date: |
Fri, 08 Mar 2013 10:43:13 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 |
Am 08.03.2013 10:13, schrieb Julien Bect:
> On 07/03/2013 20:13, John W. Eaton wrote:
>> Some things to test:
>>
>> function f (jet)
>> jet
>> end
>
> Here is what I think the answer should be (based on my own Matlab user
> experience) :
>
> f() --> not enough input arguments
> f(0) --> 0
> f(1) --> 1
>
> The fact that the symbol "jet" refers to a variable is determined IMO when
> Matlab first parses the function. Therefore, even if nargin == 0, jet is
> assumed to refer to a variable in the context of this function.
>
I can confirm that with Matlab R2011a:
??? Input argument "jet" is undefined.
Error in ==> f at 2
jet
>
>> function f (jet)
>> if (nargin == 0)
>> jet
>> elseif (nargin == 1)
>> x = jet
>> end
>> end
>
> Same thing IMO, for the same reasons.
Yes.
>
>
>> function f ()
>> jet = 1;
>> clear jet
>> jet
>> end
>
> I think this should generate an error : when the function is parsed, Matlab
> determines that "jet" refers to a local variable in this function.
>
> Therefore, the third line of code should generate an error since jet has been
> cleared.
Yes, the error is:
??? Reference to a cleared variable jet.
Error in ==> f at 4
jet
>
> Best regards,
>
> Julien.
>
>
regards
Thorsten