简体   繁体   中英

use of global variables in matlab

I have a doubt about the use of the global variables in matlab. Should I declare all the global variables in any subroutine or only when I will need it? And if a subroutine (ie function1.1) within other one (ie function1) only shares the global variable with the main programm, should I also declare them in the intermediate one (ie function1)?

I will try to clear up the case quickly with a short scheme.

Main program

global a b c

Function 1

global a

Function 1.1

global ab

Function 2

global c

Would be correct?

you only need to declare them, if you need them within the function.

But be careful with using them. You will find several post, that explain the problems with global variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM