MATLAB: Do I get the error “The PERSISTENT declaration must precede any use of the variable XXX” even though PERSISTENT is the first line of the function

MATLAB

The following is the first two lines of my function:
function out = myfun(in)
persistent out
out = true;
When I call this function, I get the following error:
The PERSISTENT declaration must precede any use of the variable out.
However, the first line of my function is the PERSISTENT declaration.

Best Answer

This bug has been fixed in Release 2007b (R2007b). For previous product releases, read below for any possible workarounds:
The error message could perhaps be more specific for this case, but is not incorrect. If you consider the first line of your function to be the function declaration statement
function out = myfun(in)
then variable ‘out’ is indeed being used before being declared in the next line as persistent.
The general rule to keep in mind here is that persistent variables cannot be used for input or output arguments to a function. This is not stated in the documentation, but will be added.