Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 07:10 02 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Micromite V5.04.09 Beta 3

     Page 2 of 2    
Author Message
Azure

Guru

Joined: 09/11/2017
Location: Australia
Posts: 446
Posted: 05:13am 22 Feb 2018
Copy link to clipboard 
Print this post

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 Zealand
Posts: 9610
Posted: 05:49am 22 Feb 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 6283
Posted: 06:06am 22 Feb 2018
Copy link to clipboard 
Print this post

  Grogster said   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.

Yes, They are LOCAL to the SUB/Function

  Grogster said  
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?


That is correct.

  Quote   ' returns the elapsed time between keypresses
PRINT timebetweendrinks()

DO
IF INKEY$<>"" THEN
PRINT timebetweendrinks(), TIME$, bottomsup, drinktime
' the two variables remaine at zero outside the function
ENDIF
LOOP


FUNCTION timebetweendrinks()
STATIC bottomsup
' we need to remember the last drink
LOCAL drinktime ' temporary variable
drinktime = VAL(MID$(TIME$,7,2))+VAL(MID$(TIME$,4,2))*60+VAL(MID$(TIME$,1,2))*3600
IF bottomsup = 0 THEN
timebetweendrinks =
0 ' gives a zero time if it is the first call to the function
ELSE
timebetweendrinks = drinktime - bottomsup
ENDIF
bottomsup = drinktime
' save the new drink time
END FUNCTION


I added the two variables STATIC and LOCAL to the print line to show that they both remain at zero outside the function.


JimEdited by TassyJim 2018-02-23
VK7JH
MMedit
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9610
Posted: 06:09am 22 Feb 2018
Copy link to clipboard 
Print this post

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: Australia
Posts: 3292
Posted: 07:36am 22 Feb 2018
Copy link to clipboard 
Print this post

  Grogster said  But if STATIC is supposed to remain across calls to subs and functions, then STATIC SwState=0 will clear it anyway

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: Thailand
Posts: 2209
Posted: 08:22am 22 Feb 2018
Copy link to clipboard 
Print this post

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.
 
     Page 2 of 2    
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025