Behavior with unspecified output arguments

5 ビュー (過去 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 Center および File ExchangeCalendar についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by