Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 22:31 04 Jul 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 II 4.6 Dim,Local & Const

Author Message
ajkw
Senior Member

Joined: 29/06/2011
Location: Australia
Posts: 290
Posted: 09:06pm 30 Jan 2015
Copy link to clipboard 
Print this post


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: Australia
Posts: 3282
Posted: 10:29pm 30 Jan 2015
Copy link to clipboard 
Print this post

  ajkw said   Is it correct that a Const is not global when created in a Sub?

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: Australia
Posts: 290
Posted: 11:18pm 30 Jan 2015
Copy link to clipboard 
Print this post

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: Thailand
Posts: 2209
Posted: 12:37am 31 Jan 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 290
Posted: 02:18am 31 Jan 2015
Copy link to clipboard 
Print this post

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: Thailand
Posts: 2209
Posted: 03:27am 31 Jan 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 290
Posted: 04:35pm 31 Jan 2015
Copy link to clipboard 
Print this post

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.

Edited by ajkw 2015-02-02
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6268
Posted: 06:27pm 31 Jan 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 290
Posted: 08:45pm 31 Jan 2015
Copy link to clipboard 
Print this post

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.Edited by ajkw 2015-02-02
 
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