[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Expected behavior for variables which shadow function names
From: |
Ben Abbott |
Subject: |
Re: Expected behavior for variables which shadow function names |
Date: |
Thu, 07 Mar 2013 14:41:04 -0500 |
On Mar 7, 2013, at 2:13 PM, John W. Eaton wrote:
> On 03/07/2013 02:04 PM, Rik wrote:
>
>> I would definitely prefer that. Can someone verify on a recent version of
>> Matlab how this behaves? If they have upgraded their behavior then we can
>> too.
>
> Some things to test:
>
> function f (jet)
> jet
> end
>
> function f (jet)
> if (nargin == 0)
> jet
> elseif (nargin == 1)
> x = jet
> end
> end
>
> call both with
>
> f (1)
> f (0)
>
> Another:
>
> function f ()
> jet = 1;
> clear jet
> jet
> end
>
> Not that it would be very hard to fix, but I hope there isn't any code in
> Octave that actually depends on the current behavior...
>
> jwe
I ran some examples. I assume you intended f(0) to be f([]) or f()?
function foo1 (jet)
jet
end
function foo2 (jet)
if (nargin == 0)
jet
elseif (nargin == 1)
x = jet
end
end
function foo3 ()
jet = 1;
clear jet
jet
end
matlab> foo1 (1)
jet =
1
matlab> foo1 ([])
jet =
[]
matlab> foo1 ()
Error using foo1 (line 2)
Not enough input arguments.
matlab> foo2 (1)
x =
1
matlab> foo2 ([])
x =
[]
matlab> foo2 ()
Error using foo2 (line 3)
Not enough input arguments.
matlab> foo3 ()
Reference to a cleared variable jet.
Error in foo3 (line 4)
jet
Ben