Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 05:50 08 Jun 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 : PicoMite V6.00.02 release candidates - all versions

     Page 51 of 51    
Author Message
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2514
Posted: 05:25am 07 Jun 2025
Copy link to clipboard 
Print this post

  Quote  When calling a subroutine you can supply less than the required number of values and in that case the missing values will be assumed to be either zero or an empty string.
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 Kingdom
Posts: 4020
Posted: 06:29am 07 Jun 2025
Copy link to clipboard 
Print this post

  phil99 said  
  Quote  When calling a subroutine you can supply less than the required number of values and in that case the missing values will be assumed to be either zero or an empty string.
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
>


Oops - looks like a bug in MMBasic!

(b should default to 0 in test.)

John
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10135
Posted: 07:13am 07 Jun 2025
Copy link to clipboard 
Print this post

  Quote  Oops - looks like a bug in MMBasic!

(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 States
Posts: 408
Posted: 07:23am 07 Jun 2025
Copy link to clipboard 
Print this post

  JohnS said  
  phil99 said  
  Quote  When calling a subroutine you can supply less than the required number of values and in that case the missing values will be assumed to be either zero or an empty string.
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
>


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: Australia
Posts: 6245
Posted: 07:24am 07 Jun 2025
Copy link to clipboard 
Print this post

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: Germany
Posts: 11
Posted: 07:28am 07 Jun 2025
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 4020
Posted: 11:38am 07 Jun 2025
Copy link to clipboard 
Print this post

  matherp said  
  Quote  Oops - looks like a bug in MMBasic!

(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
 
     Page 51 of 51    
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