![]() |
Forum Index : Microcontroller and PC projects : Final PicoMite Release - V6.00.01
![]() ![]() ![]() ![]() |
|||||
Author | Message | ||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 574 |
I think that is a bug (?) |
||||
lizby Guru ![]() Joined: 17/05/2016 Location: United StatesPosts: 3358 |
Why do you think that (a) should be the same as a? In MMBasic DOS: a=0 add1((a)) Print "main: ",a,b End Sub add1(b) b=b+1 Print "sub: ",a,b Return sub: 0 1 main: 0 0 PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
It's meant (*) to be that way. It's useful to know that it is! Easy to work around if it troubles you - use the extra parens or just don't modify things passed in as parameters. (*) let's not get into language debates... John |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6269 |
By putting 'a' in brackets '(a)', the interpreter is treating (a) as a formula A normal variable is passed by reference. When you use a formula as a parameter for a SUB, it is not passed by reference. Jim VK7JH MMedit |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2608 |
Clear_flash_2040.zip |
||||
twofingers![]() Guru ![]() Joined: 02/06/2014 Location: GermanyPosts: 1575 |
@Lizby I think you mean this? a=0 add1((a)) Print "main: (a)",a,b a=0 add1(a) Print "main: a ",a,b End Sub add1(b) Inc b Print "sub: ",,,a,b End Sub sub: 0 1 main: (a) 0 0 sub: 1 1 main: a 1 0 Kind regards Michael causality ≠ correlation ≠ coincidence |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
I like the way you've added b to the print in main! John |
||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5056 |
Hi Jim, I didn't know this. Wow, that opens a whole new universe. I know I struggled with this in several programs, always assuming the ByRef was the only way to pass data to a sub. But you need to be sharp in how to format everything. Without brackets also seems to work. mysub a 'ByRef mysub (a) 'ByRef mysub ((a)) 'ByVal mysub(a) 'function call ByRef mysub((a)) 'function call ByVal (have not checked, but I assume) Volhout Edited 2025-01-09 16:53 by Volhout PicomiteVGA PETSCII ROBOTS |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10233 |
From the manual |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7858 |
I think this makes sense in BASIC as it always evaluates bracket values first. What we are seeing is two different meanings for brackets. The first meaning is that the command has an argument, the second is that there is a value to be calculated. myfunction(A) shows this more clearly, the brackets are not optional. A is being passed by reference myfunction((A+1)) will cause the evaluator to evaluate the contents of the brackets first and replace them with a temporary value for the call. It detects that this is needed by the fact that the value to be calculated is in its own brackets. myfunction((A)) will force the evaluator to evaluate the contents as A+0 or simply A as there is nothing else, but the evaluator has been triggered so it has to be a value that is passed. I wouldn't say that this use of brackets is a "hack". I think BASIC is supposed to work in this way. FORTRAN (which has several influences on BASIC) also passes by value by default, although it uses the VALUE keyword to force passing by value. Passing values by reference can lead to errors, true, but within a SUB or FUNCTION you should be using LOCAL or STATIC variables if you want them to be private. That makes the behaviour of the routines completely predictable under all circumstances. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
I think ANSI full BASIC passes by value. (But MMBasic doesn't claim to be that.) John |
||||
bfwolf Regular Member ![]() Joined: 03/01/2025 Location: GermanyPosts: 75 |
Microsoft would it describe it like this: ![]() "We confirm this behavior not being a bug but a feature!" Some years ago when I read the MMBasic manual, I noticed, that there is no "language option" to specify whether ByRef or ByVal is requested. So I played (I think with the MSDOS version?) and looked, what happens, when I enclose argument variables with brackets - and it worked! ![]() Yes, Peter, I'm with you! It's bad style to modify parameter variables unless they are "return values" - and it's always cleaner, to make a local copy of arguments, that are no "return values", and modify those, if necessary. But it's "double safe", if the caller may protect it's arguments against modification. This would be achieved (clearly) by having ByVal. Using the "trick" with enclosing argument variables with brackets and force them being "expressions" (which could be variables, formulas or constants) works until then. At the moment I see the keywords ByVal and ByRef "nice to have".. ![]() bfwolf |
||||
zeitfest Guru ![]() Joined: 31/07/2019 Location: AustraliaPosts: 574 |
Did you mean, ... passes by reference by default ... Using values as call-by-reference arguments in calling statements can lead to ah, unfortunate happenings. In exact call-by-reference, the argument is expected to be the name of a variable, not a value. Some early language compilers were a bit vicious and would simply treat the value as a name, so say call mysub(3) would simply create and use a variable called '3', probably not the expected result. |
||||
Mixtel90![]() Guru ![]() Joined: 05/10/2019 Location: United KingdomPosts: 7858 |
Sorry, yes - FORTRAN passes by reference. I bet there's no room to implement these on the Micromite version of MMBasic. Geoff used magical unicorn power to get stuff into that! . Edited 2025-01-09 22:27 by Mixtel90 Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
bfwolf Regular Member ![]() Joined: 03/01/2025 Location: GermanyPosts: 75 |
I bet there's no room to implement these on the Micromite version of MMBasic. Geoff used magical unicorn power to get stuff into that! . Ok - if it breaks compatibility amongst different platforms, it's hard to solve. We could live with the brackets "trick". @Peter: How do you think about my FSEEK(), FREAD$(), FWRITE() ideas? ![]() I recently saw, that the present INPUT$() mostly behaves the same way as FREAD$() would. But I have now idea, how to substitute FWRITE() by PRINT #nbr, ... And the SEEK command also behaves different - not possible to seek +/- from current position or to file's end. We must track the current position ourselves then instead.. As both LittleFs and Chan's FatFS (probably used for SD-CARDs?) supply the functions to realize FSEEK(), FREAD$(), FWRITE(), I wouldn't expect much work? bfwolf |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
Bearing in mind that the features are frozen now and only bug fixes being done, you may have a wait, especially for things which are hardly important. John |
||||
bfwolf Regular Member ![]() Joined: 03/01/2025 Location: GermanyPosts: 75 |
Just made an experiment using the DOS/Windows-version (to view the resulting file): It seems, that ' Note the pending ';' ! PRINT #nbr, wdata$; just writes the data contained in wdata$ and doesn't append a CR, a TAB, a space or anything else, so this could replace the wanted FWRITE(). Can anybody confirm this behavior for other MMBasic ports? Maybe I'll write a library, having also a FOPEN() like C offers, and track the current file position with a "hidden" variable e.g. "_current_fpos". The FOPEN() then would initialize the current file position and FSEEK(), FREAD$() and FWRITE() would update it accordingly. bfwolf |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
It's standard BASIC that a ; with PRINT does that. The user manual I have even says it does that... John Edited 2025-01-10 03:40 by JohnS |
||||
bfwolf Regular Member ![]() Joined: 03/01/2025 Location: GermanyPosts: 75 |
Yes, I knew that an ending ';' supresses CR and TAB but was uncertain if perhaps a space would be added. But I think the space is leading numeric variables PRINTed? (for the sign if negative?) @Geoff, Regarding the user manual: Within the description for the OPEN statement, a WRITE statement is mentioned but never described in detail anywhere else.. Is the WRITE statement "just an alias" for PRINT or are there differences? bfwolf |
||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2608 |
AFIK there is no individual WRITE command. Write is just a component of other commands. eg I2C WRITE, SPI WRITE |
||||
![]() ![]() ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |