Filter löschen
Filter löschen

Behavior with unspecified output arguments

3 Ansichten (letzte 30 Tage)
Catalytic
Catalytic am 2 Mär. 2023
Kommentiert: John D'Errico am 2 Mär. 2023
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

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Mär. 2023
Bearbeitet: Stephen23 am 2 Mär. 2023
"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 Kommentar
John D'Errico
John D'Errico am 2 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by