MATLAB: Return -2 : is there a clean way to exit parent function from a nested function

nestedreturn

I have a parent function and several nested functions. I would like to be able, in a nested function instead of doing "return" and go back to the parent function, to do some kind of "return-2" and also exit the parent function (not only the nested one).
Is there a clean way to do it?
(I know that I can use some "flag" variable, and then in the parent function have each time "if flag; return", but I want to avoid to have duplicated code, if possible.)

Best Answer

As far as I know, the flag way you described is the clean way to do it. You dive in and then you bubble back up the reverse direction you went in. To do it any other way, for example a "goto" statement to send you directly to some arbitrary location, would enable "spaghetti code" and is not recommended. Don't worry about putting in 3 or 4 lines - your code might be way longer than that anyway. Anyway, some alternate method of accomplishing what you want would probably be that many lines also.