![]() |
Forum Index : Microcontroller and PC projects : MicroMite II 4.6 Dim,Local & Const
Author | Message | ||||
ajkw Senior Member ![]() Joined: 29/06/2011 Location: AustraliaPosts: 290 |
RE MicroMite II 4.6 In a Sub a variable DIM'd is global and LOCAL is local but a CONST is not global. Is it correct that a Const is not global when created in a Sub? Anthony. |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3282 |
Yes, that is correct. I just looked at the manual and that point is not covered, an omission I will fix. Geoff Geoff Graham - http://geoffg.net |
||||
ajkw Senior Member ![]() Joined: 29/06/2011 Location: AustraliaPosts: 290 |
Thanks Geoff, If I may however... CONST & LCONST?? Actually after writing the example below I can see than this could break existing code so perhaps CONST and GLOBALCONST but that seems messy too. I don't know, it just seemed odd you can't set a global constant in a Sub but you can a Dim.. I'll leave it to you. /EndSelfDebate Eg. Dim x initdevice 'sub to init a device do x = pin(DevicePin) print x loop Sub initdevice 'this may actually be more complex than this example LConst Somethingelse = 22 Const DevicePin = 1 Setpin DevicePin,dout,oc . . End Sub versus Dim x Const DevicePin = 1 initdevice 'sub to init a device do x = pin(DevicePin) print x loop Sub initdevice 'this may actually be more complex than this example LConst Somethingelse = 22 Setpin DevicePin,dout,oc . . End Sub |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
I am of the opinion that defining something in a sub or function that becomes global should be totally impossible. :) It is something that can only create bugs and will be extremely hard to find once programs get bigger. If you want a global variable or constant, define it globally (somewhere on the top of the program). Microblocks. Build with logic. |
||||
ajkw Senior Member ![]() Joined: 29/06/2011 Location: AustraliaPosts: 290 |
For mine if you have a Sub that has a task of initialising a device and setting up your program for the use of it then it makes some sense to define any variables or constants in it instead of defining them somewhere else in your program separated from the Sub. |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
If it makes sense to make some variabes global, then why not define them at a global level so it is clear. Anyhting defined in a sub should be local. This will allow you to reuse variable names in different sub and reuse subs in other programs and not have higher risk of breaking things when revisiting the code later and modify it by adding/removing/modifying variables from a sub. You would end up with subs that are depended on eachother and the sequence they are loaded/run. Ultimately it is up to the individual especially when code is not shared or reused. I am biased by having to do a lot of software auditing. Just an opinion. :) Microblocks. Build with logic. |
||||
ajkw Senior Member ![]() Joined: 29/06/2011 Location: AustraliaPosts: 290 |
I could go on but yes at the end of the day we can all do what we like with our own code :) I simply found it inconsistent that in a Sub you can create (Dim) a global variable while not create a global Const. Geoff has said he will fix the manual so that the behaviour is clear which is fine. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6268 |
There is always GOTO (time to duck) OPTION DEFAULT none OPTION EXPLICIT GOTO setup main: PRINT gl_test1 END setup: CONST gl_test1 = 25 GOTO main Jim VK7JH MMedit |
||||
ajkw Senior Member ![]() Joined: 29/06/2011 Location: AustraliaPosts: 290 |
Jim, I love it, others will hate it but that' their problem ![]() I am however not sure I will use it as I have essentially done away with GOTO and GOSUB but it shows there is always another way. Cheers, Anthony. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |