![]() |
Forum Index : Microcontroller and PC projects : Error: Variable type not specified...
![]() ![]() |
|||||
Author | Message | ||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4301 |
Don't try this at home then: OPTION DEFAULT NONE OPTION EXPLICIT ON DIM D$ = "HELLO" FOO(D$) PRINT D$ END SUB FOO(D$) D$ = "WORLD" END SUB Should (I hope) print "WORLD", but why if the global and parameter D$ are "separate" ? ![]() Best wishes, Tom Edited 2022-12-16 09:26 by thwill MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
Turbo46![]() Guru ![]() Joined: 24/12/2017 Location: AustraliaPosts: 1638 |
A parameter is a LOT like a LOCAL. As I have pointed out before: SUB Swap a, b LOCAL t t = a a = b b = t END SUB SUB Xch a, b, t t = a a = b b = t END SUB The two subs are interchangeable, the variables a, b and t are created when the SUB is run and disappear when it is finished. Of course in this example the type of variable is not specified. Bill Keep safe. Live long and prosper. |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
down to priorities it seems. I feel a delve into the source coming on. <smugMode>I have to say I had never thought of or do, use global names as aliases in definition. probably more luck than judgement</smugMode> you can of course defeat this by using the "ByVal" trick : replace FOO(D$) with FOO((D$)) in line 5 @Turbo46 using a global variable is a nice cheat to get more than one value from a Function(). In the example you give (with global t) I imagine it will execute faster because there is no house-keeping you get associated with LOCALs. Possible a nice speed tweak on low-power systems... just tried that... a *very* healthy speed increase avoiding the LOCAL option: > cpu 48 > list Dim integer n,a,b,t Timer=0 For n=1 To 10000 Swap Next Print Timer Timer=0 For n=1 To 10000 Xch Next Print Timer Sub Swap Local t t = a a = b b = t End Sub Sub Xch t = a a = b b = t End Sub > run 3393 2532 > ~25% speed increase with defaulted floats and > RUN 3186 2532 > ~20% with defaulted integers that is not inconsiderable... of course this only pays when you are calling a Sub over and over... if there are "human time-frame" times between calls you won't see the 300uS involved each time. Edited 2022-12-16 19:17 by CaptainBoing |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4034 |
It's a sort of quirk. MMBasic (unlike many, maybe most, languages) passes arguments by "reference" rather than by "value". The "value" case is where a sort of hidden temporary holder is created and passed in, so if the SUB/FUNCTION alters it there's no change in the calling environment. You can simulate that in MMBasic by passing in an expression, such as extra parentheses or a calculation (even if you just add 0 or an empty string ""). The "reference" situation means that enough is passed in by MMBasic that the SUB/FUNCTION can alter the argument i.e. changes are seen by the caller. Other languages typically are value by default and need something extra to pass in by reference (e.g. in C you explicitly pass in a pointer and the function also has to state it is to receive a pointer). The MMBasic case is somewhat dangerous and I suspect will catch some people out, but it is what it is. John |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4301 |
It's a case of getting used to it. I find it very useful when I need to return multiple values from a "function" or get a CSUB to return a value otherwise it would need global variables - and then woe betide if you need recursive function call. You just need to be wary of manipulating "in" parameters just because it is convenient to do so. Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |