![]() |
Forum Index : Microcontroller and PC projects : COMMON statement in Basic ?
Author | Message | ||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 574 |
Just wondering, Is there an equivalent of a COMMON statement in Basic ? (assuming it was not banned by public demand ![]() eg simplistic level of unnamed common PROGRAM demcommon INTEGER*4 a, b, c COMMON a, b, c PRINT "Start .." a = 17 b = 3 c = 44 PRINT "Initial values" PRINT a," ", b," ", c CALL doit ( ) PRINT "Final values" PRINT a," ", b," ", c PRINT "Ok finish " END SUBROUTINE doit ( ) INTEGER*4 g, j, k COMMON g, j, k g = 12 j = 9 k = 25 PRINT "Subroutine values" PRINT g," ", j," ", k RETURN END \ Start .. Initial values 17 3 44 Subroutine values 12 9 25 Final values 12 9 25 Ok finish |
||||
PhenixRising Guru ![]() Joined: 07/11/2023 Location: United KingdomPosts: 1361 |
Misunderstood ![]() Edited 2024-11-28 21:52 by PhenixRising |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3285 |
I'm not sure exactly what a COMMON statement is supposed to do and your program assumes that the reader knows this vital fact. Can you explain in words? Geoff Geoff Graham - http://geoffg.net |
||||
thwill![]() Guru ![]() Joined: 16/09/2019 Location: United KingdomPosts: 4302 |
I'm not sure it is meaningful to BASIC; COMMON blocks are Fortran's equivalent to global variables. Best wishes, Tom MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5059 |
Then the equivalent of COMMON would be DIM As oppose to LOCAL Volhout PicomiteVGA PETSCII ROBOTS |
||||
CaptainBoing![]() Guru ![]() Joined: 07/09/2016 Location: United KingdomPosts: 2170 |
I don't think the Fortran concept of COMMON translates directly into Basic due to differences in the concepts of varaibles and "blocks" between the two languages. If it helps; In MMBasic, all variables declared with a DIM (and CONSTs) are "common" - i.e. accessible from all parts of your program. Even if you DIM them inside a SUB/FUNCTION (S/F). LOCAL variables are only allowed inside a S/F (and must be declared there). A variant of LOCAL is STATIC. Local variables are thrown away when you leave the "parent" S/F e.g. numerics go back to 0 and strings to ""... STATICs don't. They are still only available to the S/F but retain their values when you leave so they are the same when you come back as when you left the S/F so... Option Explicit' a good habit - variables should always have explicit creation Dim Integer a' I'm usable anywhere MySub Print a ' will give 7 because was set inside MySub Print b ' will give 49 (a*a) Print c,d ' causes and error because these variables are unknown outside of MySub Sub Mysub Dim Integer b' I'm also usable anywhere - a new "common" variable Local Integer c' I only work inside MySub Static Integer d' so do I but I remember my last value a=7 ' setting a "common" variable b=a*a ' working with "common" variables inside a Sub End Sub also, note that variables are passed into S/F "ByRef" (i.e. via their pointer) and there is no equivalent to "ByVal" of VB. There's a discussion on how to achieve either and some features you can exploit, here Edited 2024-11-28 23:19 by CaptainBoing |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
In the example, there is one (unnamed) COMMON area, with 3 variables which are renamed such that a&g share the same memory, as do b&j and c&k. I think it can't be done neatly in BASIC. And why would you want to do so??? It's a bit of an ugly hangover from the days of experimenting with program features, reducing memory footprints on incredibly costly computers with tiny memories (*), etc. (*) in the example, it would save passing a,b,c in as parameters; there's not really a good reason to rename them - would be better to use sane names in the first place and stick to them hmm, thinking a bit more, it's quite a good example to put people off using that language... John Edited 2024-11-29 02:17 by JohnS |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7871 |
Isn't COMMON redundant in a language where all variables are global by default? Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
Well... The program in question could be built from several parts e.g. a library routine has the COMMON and other parts use it. It's such a silly example, really, I'd say, so is it really worth wondering how to do it in BASIC - especially as you just wouldn't! Oh, my FORTRAN knowledge has been unused for >40 years so apologies if I get something wrong. John Edited 2024-11-29 03:02 by JohnS |
||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 574 |
Thanks everyone ! "It's a bit of an ugly hangover" ![]() I agree, its a bit prehistoric in IT terms. [ My apologies, I typed a more extensive explanation/reply but TBS logged me out and it was lost ![]() Edited 2024-11-29 10:48 by zeitfest |
||||
toml_12953 Guru ![]() Joined: 13/02/2015 Location: United StatesPosts: 427 |
In FORTRAN, COMMON allows passing variables between programs. Anything in a COMMON block is retained when a new program is loaded. The new program can have a similar COMMON block to accept the variables. The names in the COMMON block can be different, but the types have to match (the same rules as in passing parameters to a SUB or FUNCTION) In BASIC, is might be something like this: Calling program: COMMON A, Nam$, Velocity Called program: COMMON B, MyName$, Speed |
||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 574 |
[ ahhh ! cool weather again ![]() So - in Basic variables are global by default unless they are declared as local. I can see that would be useful in many situations. In some languages, variables used in subroutines are assumed to be local and are stored in additional memory as usual, unless they are passed by reference, in which case they map to the same memory locations as the variables cited in the call. It can have some advantages. One of the aims of using subroutines is to allow re-usable code, ie the code can be largely copied in. To keep it all robust the variables used should then be local in scope - they are then not global and parameters have to be used to pass information. But a complex subroutine can need a lot of info to be be passed to/from it, which then can require a lot of parameters in the calling statements and definitions - which can get quite unwieldy. So COMMON was used instead of using large lists of parameters in calls. It mapped multiple variables in multiple subroutines to use the same memory in common, hence the name. Multiple areas of memory could be used, with optionally an initial un-named block and further named blocks each with separate sets of variables. It could get very messy and is probably best not used ![]() I guess Basic then sticks with the large parameter list method ? Edited 2024-12-09 12:34 by zeitfest |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5059 |
Thank you for the explanation. In MMBasic the way to transfer data between programs would be to write them to a file on the A: drive. The other program can read them back. A: is a flash drive that is quite fast (faster than the SD card). Alsternatively (just when the second program is in one of the 3 flash banks) you can use VAR SAVE. VAR SAVE saves to a 16k memory block that is non volatile. But it gets erased when you load a new program. However, if your 2nd program runs in flash, you do not have to load it. The VAR SAVE however saves variables including the name. Your second program has to use the same name. Volhout PicomiteVGA PETSCII ROBOTS |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |