Script vs Function with no input/output
显示 更早的评论
Sometimes I have to write a function ad-hoc for a script purpose. Usually I was simply creating a new .m file containing the function.
In order to increase the robustness of my code and avoid a crowd of small .m files roaming around i'm starting considering to exploit the ability of function files to include sub-functions directly in their inside. To exploit this ability however I have to declare the whole script as a function with no input and no output.
Therefore, instead of having:
- Script.m
a=1;
b=2;
c=fun(a,b);
- fun.m
function c=fun(a,b)
c=a+b;
I create a single file
- Script.m
function script()
a=1;
b=2;
c=fun(a,b);
function c=fun(a,b)
c=a+b;
Of course in this case it seems useless. But sometimes this allows to have only one file instead of 5, 10 or maybe even more that can be shared more easily and are more "compact". (Indeed this kind of behavior can be done natively in other languages such as Python)
Now my question is: except for the "usual" care that have to be put in place when debugging a function and not a script due to the different workspace the variable resides into; is there any drawback you can spot?
Thank you for your opinion. Regards, Luca
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!