![]() |
Forum Index : Microcontroller and PC projects : PicoMite V6.00.02 release candidates - all versions
![]() ![]() |
|||||
Author | Message | ||||
phil99![]() Guru ![]() Joined: 11/02/2018 Location: AustraliaPosts: 2514 |
BYVAL appears not to adhere to that. > LIST ' Test prog 1 Dim integer a=5, b=6, c=7 Print "a, b, c =";a;b;c test a,,c Sub test(BYVAL a, BYVAL b, BYVAL c) If a = 5 Then a = 10 Print "In Sub a, b, c =";a;b;c EndIf End Sub Print "After Sub a, b, c =";a;b;c > RUN a, b, c = 5 6 7 In Sub a, b, c = 10 6 7 After Sub a, b, c = 5 6 7 > > LIST ' Test prog 1 Dim integer a=5, b=6, c=7 Print "a, b, c =";a;b;c test a,,c Sub test(a, b, c) If a = 5 Then a = 10 Print "In Sub a, b, c =";a;b;c EndIf End Sub Print "After Sub a, b, c =";a;b;c > RUN a, b, c = 5 6 7 In Sub a, b, c = 10 0 7 After Sub a, b, c = 5 6 7 > BYVAL appears to be the default anyway. Senility advancing. Edited 2025-06-07 16:01 by phil99 |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4020 |
Oops - looks like a bug in MMBasic! (b should default to 0 in test.) John |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10135 |
(b should default to 0 in test.) No it isn't. You are just printing the value of a global variable which wasn't passed. Try ' Test prog 1 Dim integer a=5, b=6, c=7 Print "a, b, c =";a;b;c test a,,c Sub test(BYVAL x%, BYVAL y%, BYVAL z%) If x% = 5 Then x% = 10 Print "In Sub x% y%, z% =";x%;y%;z% EndIf End Sub Print "After Sub a, b, c =";a;b;c > RUN a, b, c = 5 6 7 In Sub a, b, c = 10 6 7 After Sub a, b, c = 5 6 7 > |
||||
toml_12953 Guru ![]() Joined: 13/02/2015 Location: United StatesPosts: 408 |
> LIST ' Test prog 1 Dim integer a=5, b=6, c=7 Print "a, b, c =";a;b;c test a,,c Sub test(BYVAL a, BYVAL b, BYVAL c) If a = 5 Then a = 10 Print "In Sub a, b, c =";a;b;c EndIf End Sub Print "After Sub a, b, c =";a;b;c > RUN a, b, c = 5 6 7 In Sub a, b, c = 10 6 7 After Sub a, b, c = 5 6 7 > Oops - looks like a bug in MMBasic! (b should default to 0 in test.) John It doesn't look like a bug to me. Variables that aren't passed are global unless declared as LOCAL. The variable b wasn't passed so it retained its value of 6. Try changing b in the sub and see what happens. It will have the changed value after exiting the sub. |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6245 |
If you use OPTION EXPLICIT RUN a, b, c = 5 6 7 In Sub x% y%, z% = 10 [10] Print "In Sub x% y%, z% =";x%;y%;z% Error : Y is not declared > Perhaps a warning in the manual to take care with empty parameters and BYVAL Jim VK7JH MMedit |
||||
AlbertR Newbie ![]() Joined: 29/05/2025 Location: GermanyPosts: 11 |
there should also be a different if the "AS INTEGER" is used Option EXPLICIT Dim integer a a = 2 Print "a at start "a test1 Print "test without par:"a a = 2 test1(a) Print "test1: "a a = 2 test2(a) Print "test2: "a a = 2 test3(a) Print "test3: "a a = 2 test4(a) Print "test4: "a a = 2 test5(a) Print "test5: "a a = 2 test6(a) Print "test6: "a '------------------------------------ Sub test1( b ) If b = 2 Then b = 23 End Sub Sub test2( b As integer ) If b = 2 Then b = 23 End Sub Sub test3(byval b ) If b = 2 Then b = 23 End Sub Sub test4(byval b As integer) If b = 2 Then b = 23 End Sub Sub test5(byref b As integer) If b = 2 Then b = 23 End Sub Sub test6(byref b ) If b = 2 Then b = 23 End Sub >RUN a at start 2 test without par: 2 test1: 2 test2: 23 test3: 2 test4: 2 test5: 23 [23] test6(a) Error : BYREF requires same types: a |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4020 |
(b should default to 0 in test.) No it isn't. You are just printing the value of a global variable which wasn't passed. Try ' Test prog 1 Dim integer a=5, b=6, c=7 Print "a, b, c =";a;b;c test a,,c Sub test(BYVAL x%, BYVAL y%, BYVAL z%) If x% = 5 Then x% = 10 Print "In Sub x% y%, z% =";x%;y%;z% EndIf End Sub Print "After Sub a, b, c =";a;b;c > RUN a, b, c = 5 6 7 In Sub a, b, c = 10 6 7 After Sub a, b, c = 5 6 7 > I don't see how that can print In Sub a, b, c = 10 6 7 as it should be In Sub x% y%, z% etc But addressing the main issue: normally defining a SUB with parameters makes each of those parameters a sort of locally defined variable, so the original example would have both a global variable b and a parameter b. The parameter would in effect hide the global one. It's OK if that is not how MMBasic is supposed to work but it is most unusual/odd. John Edited 2025-06-07 21:38 by JohnS |
||||
![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |