Behavior with unspecified output arguments
조회 수: 2 (최근 30일)
이전 댓글 표시
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?
c=func(0,0)
function t=func(a,b)
if b>0
t=a+b;
end
end
댓글 수: 0
채택된 답변
Stephen23
2023년 3월 2일
편집: Stephen23
2023년 3월 2일
"Has it always been this way?"
Yes.
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
else
xx = x;
end
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
John D'Errico
2023년 3월 2일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!