![]() |
Forum Index : Microcontroller and PC projects : Micromite V5.04.09 Beta 3
![]() ![]() |
|||||
Author | Message | ||||
Azure![]() Guru ![]() Joined: 09/11/2017 Location: AustraliaPosts: 446 |
Think of STATIC as a DIM that is only visible to the SUB/FUNCTION it is declared in. Great for writing portable routines that need to preserve information between each time it is called. |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9610 |
Can I please just confirm that STATIC variables are only visible to the routine that called it? IE: NOT visible to the global code, and also NOT visible to OTHER sub's that do other things. STATIC is 'static' to the routine that created it in the first place, in other words. If you have a STATIC variable, it obviously is saved to memory somewhere when the sub/function exits. When the sub/function is called again at some point, and the interpreter runs into the STATIC keyword, I assume it then preloads the variable names that match the ones it has stashed in memory somewhere. Is that how it is working? Smoke makes things work. When the smoke gets out, it stops! |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Yes, They are LOCAL to the SUB/Function That is correct. I added the two variables STATIC and LOCAL to the print line to show that they both remain at zero outside the function. Jim VK7JH MMedit |
||||
Grogster![]() Admin Group ![]() Joined: 31/12/2012 Location: New ZealandPosts: 9610 |
Marvellous, thanks. ![]() I think I comprehend now. Smoke makes things work. When the smoke gets out, it stops! |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
No, STATIC SwState=0 will just set the initial value for the variable for the first call to the subroutine. On subsequent calls its value will be whatever its last value was. Actually, setting it to zero was redundant as all variables are set to zero when created (including STATIC variables). I can see that this could cause confusion so I will make sure that the documentation covers it. Geoff Geoff Graham - http://geoffg.net |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
Static in computer languages means that it is allocated only once and that allocation never changes (remains static) for the entire run of the program, it is also initialized only once. Exactly the same as a global variable although those can be done with DIM. A LOCAL variable will be allocated (storage room for the variable in memory) each time a sub or function is called and it is also initialized each time. LOCAL has scope, meaning that variable is only available within the sub or function. STATIC is a variation of LOCAL. It is still only available within the sub or function but the storage allocation and initial value is done only once. Microblocks. Build with logic. |
||||
![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |