Page 1 of 1
[Implemented] Static variables
Posted: Tue Oct 08, 2002 11:26 pm
by BackupUser
Restored from previous forum. Originally posted by Holger.
I'd like to see static variables in procedures.
Procedure.l Change()
static a.l = 5
a = a + 1
ProcedureReturn a
EndProcedure
result.l = Change()
PrintNumberN(result)
result.l = Change()
PrintNumberN(result)
should print 5 and 6
This way you could remember some state without using global variables.
Even more nice would be to have static vars that have visibilty scope in exactly the source file where they are defined (even if included?). Would give us some object oriented feeling without much burden.
Posted: Wed Oct 09, 2002 7:10 am
by BackupUser
Restored from previous forum. Originally posted by waffle.
yes, i miss using static vars inside functions
Posted: Sat Mar 29, 2003 9:07 am
by BackupUser
Restored from previous forum. Originally posted by Amiga5k.
Yes, we have Global, Shared and Pretected, so all we're missing is Static. These really come in handy for procedures that are called frequently, like once per loop to process some music, etc, without having to use dozens of Global variables.
***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***
Posted: Sat Mar 29, 2003 2:11 pm
by BackupUser
Restored from previous forum. Originally posted by ebs.
Holger,
I agree with you about the need for static variables, but shouldn't your example print 6 and 7, not 5 and 6?
Eric
Originally posted by Holger
I'd like to see static variables in procedures.
Procedure.l Change()
static a.l = 5
a = a + 1
ProcedureReturn a
EndProcedure
result.l = Change()
PrintNumberN(result)
result.l = Change()
PrintNumberN(result)
should print 5 and 6
This way you could remember some state without using global variables.
Even more nice would be to have static vars that have visibilty scope in exactly the source file where they are defined (even if included?). Would give us some object oriented feeling without much burden.
Posted: Sat Mar 29, 2003 8:05 pm
by BackupUser
Restored from previous forum. Originally posted by Amiga5k.
Actually, I think it would print 6 and 6, since a is set to 5 each time and then incremented by one. There would have to be some switch put in there that would check to see if it had already been defined I guess. Unless Static assigns an initial value to a the first time through (5) and then ignores the 'Static x = x' line on subsequent times through. If that's the case, and actually I think it is, then the results would indeed be 5 and 6. (In Powerbasic, Static allows you to set it's type , but not it's value on the same line: 'Static x AS INTEGER').
Russell
***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***
Posted: Sat Mar 29, 2003 8:46 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Amiga5k
Unless Static assigns an initial value to a the first time through (5) and then ignores the 'Static x = x' line on subsequent times through. If that's the case, and actually I think it is, then
That's how it works in C.
the results would indeed be 5 and 6.
No, it really would be 6 and 7. It's set to 5 and gets increased before it gets printed.
--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
Posted: Sun Mar 30, 2003 10:06 pm
by BackupUser
Restored from previous forum. Originally posted by Amiga5k.
You're right on this one, Tinman. I realized it right after I posted it, but found no way to edit once it was there

. (Should have used Preview instead...).
Russell
***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***
Posted: Sun Mar 30, 2003 11:01 pm
by BackupUser
Restored from previous forum. Originally posted by tinman.
Originally posted by Amiga5k
You're right on this one, Tinman. I realized it right after I posted it, but found no way to edit once it was there

. (Should have used Preview instead...).
You should be able to edit your posts by clicking on the icon that looks like a pencil over a piece of paper at the top of your post. You need to be logged in before you look at the page which contains your post or the icon won't show up.
--
I used to be a nihilist but I don't believe in that any more.
(Win98first ed. + all updates, PB3.62, external editor)
Posted: Tue Apr 01, 2003 9:26 pm
by BackupUser
Restored from previous forum. Originally posted by Amiga5k.
Ah, works now! In the past, I've had problems with the forum where I'd log in, and then after I read a post, I'd have to re-log in every time (it wouldn't remember that I was logged in already), so I'd just manually log in after clicking on "Reply to Topic". This is much better
Russell
***Commodore 64 - Over one million cycles per second, 16 vibrant colors, 3 incredible audio channels and 38,911 Basic Bytes Free! Who could ask for anything more?***