Unexpected behaviour when calling main function with 2 outputs

1 次查看(过去 30 天)
For this function, I'm expecting 2 outputs when I run the script (n,p). However, I only get one 'n' when I run. Why?
function [n, p] = SandboxFile()
clear all
clc
n = 5;
p = 6;
end
  3 个评论
Walter Roberson
Walter Roberson 2019-3-24
I am wondering why Matlab is not able to recognize the number of outputs and return me all those outputs instead of manually having to type in the command window n,p.
The reason that it cannot do that is that there is no connection between the dummy variable names used as the output variables in the function, and the names of similar variables in the workspace of the caller.
The entire purpose of functions is encapsulation so that the implementation of the function is hidden and cannot interfere with the calling function. The calling function should not have to know or care whether the called function uses particular internal variable names.
Consider the sequence
abc = 123;
some_function();
disp(abc)
In any reasonable programming language, provided the implementator of some_function does not deliberately override normal operations, abc would still be 123 after some_function() is called no matter which variables some_function uses internally. The result of those statements should not vary if the interface definition is
function abc = some_function()
compared to if the interface definition is
function av34va_3uja = some_function()
If, as you propose, it did matter, and that abc or av34va_3uja would be assigned to in the calling function if the user did not specify all of the destination variables, then in order to deliberately write safe code it would be necessary to predict which variable names that the calling function might accidentally use internally, in order to avoid writing over those variables when the user did not want that. And woe is you if you are making recursive calls and so cannot possibly avoid having an output variable name that is used in the caller.
WIth the current definition of MATLAB, there is no chance of accidentally overwriting a variable that the user did not ask to be assigned to -- not unless the person writing the function deliberately uses assignin('caller') (or you use global variables.) With the definition of MATLAB that you propose, you would have to go to lengths to prevent accidentally overwriting variables in the caller. It is not a good scene.

请先登录,再进行评论。

采纳的回答

Stephan
Stephan 2019-3-24
编辑:Stephan 2019-3-24
Call it with 2 output elements:
[n, p] = SandboxFile()
Sure about
clc
clear all
? This is unneccassary and can cause problems.
  2 个评论
N/A
N/A 2019-3-24
Can you provide some more details about what problems it may cause?
Walter Roberson
Walter Roberson 2019-3-24
"clear all" removes all persistent variables in all functions, and removes all global variables, and removes the cached pre-parsed versions of all functions (so they have to be re-learn their JIT optimal behaviour.) It does not, however, remove any existing graphics objects.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Lighting, Transparency, and Shading 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by