View Full Version : STATIC variables
ErosOlmi
24-03-2008, 23:53
Next thinBasic version will have STATIC variables developed.
STATIC variables are variables defined inside FUNCTIONs or SUBs, have local scope (are seen only from inside the function or sub that define it) and maintain theyr value even when function or sub finish its execution.
You will be able to define any STATIC variable of whatever type inside a function or sub.
Regards
Eros
UPDATE:
2008.03.25
Attached a new thinCore version implementing STATIC variables
Also in the zip file an example
2008.03.26
Attached file updated. Fixed STATIC variables passed as function parameters to other function
Attached file updated. Fixed recursive function using STATIC variables
2008.03.27
fix &b (lower case) used to manage binary number representation
THanks Eros, that is a great feature!
Petr Schreiber
25-03-2008, 09:07
Very nice feature,
they can eliminate need for global variables in some cases very nicely,
I am looking forward to it!
Thanks,
Petr
ErosOlmi
25-03-2008, 20:06
OK, attached to the first post of this thread a new thinCore.dll thinBasic Core engine implementing STATIC variables.
Still under testing so if you find any trouble please let me know.
Remember: static variables are variables that persists for the life of the running process and have function scope, that is are visible only inside the function implementing them.
Here an example:
'----------------------------------------------------------------------------
' Using static variables
'----------------------------------------------------------------------------
uses "console"
function Test001() as long
'--- Define s static variable that will be used as function execution counter
'--- Every time function is executed, counter is incremented
'--- Static variables are declared only once and are local to the function.
'--- Static variables retain their values as long as the program is running
static Counter as long
'--- Now increment by 1
incr Counter
console_writeline "Counter Inside " & function_name & " is: " & format$(Counter, "000")
end function
function Test002(optional byval SetString as string) as long
'--- Now a test with 2 static variables: one counter and one string array
static Counter as long
static MyArrayOfStrings(10) as string
incr Counter
if Counter <= ubound(MyArrayOfStrings) then
MyArrayOfStrings(Counter) = SetString
console_writeline "String in function " & function_name & " is: " & MyArrayOfStrings(Counter)
end if
end function
'---
dim Counter as double
dim RetValue as long
for Counter = 10 to 1 step -1
console_write "Counter outside is: " & format$(Counter, "000") & " - "
Test001
Test002("Any string " & Counter)
next
console_writeline "All done. Press any key to continue"
console_waitkey
Petr Schreiber
25-03-2008, 20:42
Very nice!,
I originally thought you just set special access flags for GLOBALs, but more I think of it, more complex task it had to be.
Thanks a lot,
Petr
P.S. Testing so far goes well, no problems found
ErosOlmi
25-03-2008, 22:04
I originally thought you just set special access flags for GLOBALs, ...
;) that was my original idea when I had no ... ideas :D
Than I get a flash and found the way to go.
thinBasic static variables are real static and created in a variable scope connected with the function in which static are declared.
I've not tested all situations so I expect there are some bugs but so far it seems working fine.
I still have to test situations like calling a function with static that call another function that have static or calling recursively a function with static.
Ciao
Eros
Petr Schreiber
25-03-2008, 22:29
Hi Eros,
here is one problematic case - passing STATIC as parameter to other function
I get "Variable not defined or mispelled Keyword"
uses "console"
alias console_printline as print
a(5)
console_waitkey
sub a( param as long )
print "-- SUB A"
static myVar as long
myVar = param
b(myVar)
end sub
sub b( param as long )
print "-- SUB B"+STR$(param)
end sub
Thanks,
Petr
ErosOlmi
25-03-2008, 22:32
I knew you were a real bug buster!
:P
Amazing Petr how you seem to be able to find bugs so quickly.
ErosOlmi
26-03-2008, 01:09
In reality there are some other bugs for example when a function with static is called many times.
:( Well, some other job to do on STATIC matter but absolutely worth to have them.
ErosOlmi
26-03-2008, 03:50
here is one problematic case - passing STATIC as parameter to other function
Problem should be fixed. Get updated attached file in first post of this thread.
Ciao
Eros
Petr Schreiber
26-03-2008, 09:47
Thanks Eros,
the bug was easy to find only thanks to your mention about recursion :)
The new version corrected the sample above very well, but I found one more problem, now with that recursion.
It seems STATIC variable can lose its value?
uses "console"
alias console_printline as print
a(1)
console_waitkey
sub a( param as long )
static myVar as long
myVar = param
incr myVar
print $SPC(myVar)+"RECURSE A >>" +STR$(myVar)
if param < 10 then a(myVar)
print $SPC(myVar)+"RECURSE A <<" +STR$(myVar)
end sub
If I would use LOCAL, the output would be correctly:
RECURSE A >> 2
RECURSE A >> 3
RECURSE A >> 4
RECURSE A >> 5
RECURSE A >> 6
RECURSE A >> 7
RECURSE A >> 8
RECURSE A >> 9
RECURSE A >> 10
RECURSE A >> 11
RECURSE A << 11
RECURSE A << 10
RECURSE A << 9
RECURSE A << 8
RECURSE A << 7
RECURSE A << 6
RECURSE A << 5
RECURSE A << 4
RECURSE A << 3
RECURSE A << 2
But with that static I get weird results:
RECURSE A >> 2
RECURSE A >> 3
RECURSE A >> 4
RECURSE A >> 5
RECURSE A >> 6
RECURSE A >> 7
RECURSE A >> 8
RECURSE A >> 9
RECURSE A >> 10
RECURSE A >> 11
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
RECURSE A << 0
I think it should give the same as first listing.
EDIT: No it should not :D
Thanks,
Petr
ErosOlmi
26-03-2008, 15:21
New version attached into first post of this thread.
Fixed recursive calling of functions containing.
Now I want to see what you find ! ;D
Petr Schreiber
26-03-2008, 16:27
Thanks Eros,
works great, beware that your cruel treatment of bugs could be spotted by "friends of nature" :D
Thanks,
Petr
Michael Clease
26-03-2008, 16:34
Perhaps you could add "Kills all KNOWN bugs" to the slogans ;D
ErosOlmi
26-03-2008, 17:45
:D :D
Michael Hartlef
26-03-2008, 22:00
Thanks Eros, STATIC variables sound very usefull.