Hi,
I have a primary function set with 12 global variables I want the sub-functions to use, they are only double constants held during a loop.
Apart from being typing lazy (copy/past) and prob bad programming, is there any programming problem or reason why I should not declare a global variable in a sub-function that is not used?
Thanks
AD

 採用された回答

Paulo Silva
Paulo Silva 2011 年 12 月 6 日

4 投票

The variables should be passed to the functions as arguments, using global variables always introduce problems that are hard to debug, also if you declare a variable on a function you can access the same variable in every sub-function of that function.
Now for your question, besides the use of global variables and that sub-function that's not used?! I don't see any other problem

3 件のコメント

Sean de Wolski
Sean de Wolski 2011 年 12 月 6 日
+1, but I think you meant nested functions can see the variables, subfunctions need them to be passed in.
Paulo Silva
Paulo Silva 2011 年 12 月 6 日
Sean you are correct, thanks, I always confuse them, there's a nice video tutorial from Doug Hull http://blogs.mathworks.com/videos/2011/09/20/managing-code-in-matlab-nested-sub-functions
Scragmore
Scragmore 2011 年 12 月 6 日
Thanks, it was as I thought. Laziness on my part, not wanting to custom build large passes to a function. As well as readability and good programing practice. I think I managed to look for a compromise, declaring global sudo constants for large scale function structures and passing local variables.
Thanks for the clarification.
AD

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Scope Variables and Generate Names についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by