Behavior with unspecified output arguments

6 views (last 30 days)
I am a bit puzzled by the behavior I see in the below example. I thought Matlab will always check that a function call assigns any explicit outputs that it defines. Because the signature line of func() does not use varargout, but instead the explicit output t, shouldn't an error message occur in the first line even though func is not called with an explicit output argument? Has it always been this way?
func(0,0) % Why doesn't this produce an error?
Output argument "t" (and possibly others) not assigned a value in the execution with "solution>func" function.
function t=func(a,b)
if b>0

Accepted Answer

Stephen23 on 2 Mar 2023
Edited: Stephen23 on 2 Mar 2023
"Has it always been this way?"
Several MATLAB functions from waaaay back in the past use exactly this feature. For example, CALENDAR right from before R2009 up until today includes these lines of code:
function xx = calendar(c,m)
if nargout==0,
.. display here
xx = x;
So calling it with no explicit output argument it will display the calendar, but an explicit output returns array xx.
I have also used this feature for many years, exactly like CALENDAR: if zero outputs then display something... and when doing so, who wants a superfluous ANS being generated? Ugh, no thanks!
You can think of ANS as being optional: it will be returned if it can be, otherwise well... MATLAB just moves on :)
  1 Comment
John D'Errico
John D'Errico on 2 Mar 2023
I can't claim first hand if it has always been that way. But I can say that it has been that way for at least 35 years, since I started using MATLAB heavily. So maybe version 3 or so, before they even called them releases.

Sign in to comment.

More Answers (0)





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!