Behavior with unspecified output arguments

조회 수: 2 (최근 30일)
Catalytic
Catalytic 2023년 3월 2일
댓글: John D'Errico 2023년 3월 2일
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)
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
t=a+b;
end
end

채택된 답변

Stephen23
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
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by