Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:36 01 Aug 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 : MM Basic - passing by reference help

Author Message
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 01:29pm 22 Jan 2021
Copy link to clipboard 
Print this post

I have written code to check for palindromic hex and binary numbers.  Except it doesnt work -- due to passing by reference and I can't find a way out of it:

Function reverse(in_string$) As string
   For i = Len(in_string$) To 1 Step -1
       b$=Mid$(in_string$,i,1)
       r$=r$+b$
   Next i
   reverse$=r$
End Function


For i = 1 To 100
   If Bin$(i) = reverse(Bin$(i)) Then
       If Hex$(i) = reverse(Hex$(i)) Then
           Print i,Bin$(i), Hex$(i)
       EndIf
   EndIf
Next i


The function works fine (outside of the for loop)  as does the hex / bin conversions.  The link between the loop and the function breaks and this leads me to think is a passing issue (by reference?)

Any minds greater than mind have a look at this for me please?

Nim
Edited 2021-01-22 23:30 by Nimue
Entropy is not what it used to be
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 01:36pm 22 Jan 2021
Copy link to clipboard 
Print this post

Hi Nimue,

Unfortunately I'm not near my CMM2 so can't run it for myself.

I can't see an obvious pass-by-reference issue; reverse() doesn't do any "write operations" to in_string$.

I'm a little suspicious about the reverse$=r$ line when the Function is called reverse not reverse$, but that might be harmless. I generally don't mix the !%$ suffixes with the AS form.

How does it not work? Can we have some sample output.

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 01:52pm 22 Jan 2021
Copy link to clipboard 
Print this post

  thwill said  Hi Nimue,

Unfortunately I'm not near my CMM2 so can't run it for myself.

I can't see an obvious pass-by-reference issue; reverse() doesn't do any "write operations" to in_string$.

I'm a little suspicious about the reverse$=r$ line when the Function is called reverse not reverse$, but that might be harmless. I generally don't mix the !%$ suffixes with the AS form.

How does it not work? Can we have some sample output.

Tom


What I get is "String too long".

If I add an extra print in the For Loop:

For i = 1 To 100
   Print i  ' Debug
   If Bin$(i) = reverse(Bin$(x)) Then
       If Hex$(i) = reverse(Hex$(i)) Then
           Print i,Bin$(i), Hex$(i)
       EndIf
   EndIf
Next i


I get:
1
1
1
1
1
1
1
etc
etc with the loop not incrementing until the string error.

Exactly the same happened before on a project where passing was the issue IIRC.

The reverse function works well on its own:

print reverse("Nimue") produces the desired output of a reversed string.

N
Entropy is not what it used to be
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 01:56pm 22 Jan 2021
Copy link to clipboard 
Print this post

The first obvious bug is that i and r$ are global variables and are never re-initialised after the first time it is used. i in particular is buggered by the subroutine

USE OPTION EXPLICIT!!!!!!!!!
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 01:58pm 22 Jan 2021
Copy link to clipboard 
Print this post

  matherp said  The first obvious bug is that i and r$ are global variables and are never re-initialised after the first time it is used. i in particular is buggered by the subroutine

USE OPTION EXPLICIT!!!!!!!!!


That will be it ;-) That will teach me to ignore the obvious.

Edit: Languages with implicit declaration are a 'mare ... and personally I don't have anything good to say about languages with implicit typing.
Edited 2021-01-23 00:02 by thwill
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 533
Posted: 02:00pm 22 Jan 2021
Copy link to clipboard 
Print this post

I'm missing the bold part.

Function reverse(in_string$) As string
  r$ = ""
  For i = Len(in_string$) To 1 Step -1
      b$=Mid$(in_string$,i,1)
      r$=r$+b$
  Next i
  reverse$=r$
End Function


For i = 1 To 100
  If Bin$(i) = reverse(Bin$(i)) Then
      If Hex$(i) = reverse(Hex$(i)) Then
          Print i,Bin$(i), Hex$(i)
      EndIf
  EndIf
Next i


Or simpler:

Function reverse(in_string$) As string
  reverse = ""
  For i = Len(in_string$) To 1 Step -1
      CAT reverse, Mid$(in_string$,i,1)
  Next i
End Function

Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
thwill

Guru

Joined: 16/09/2019
Location: United Kingdom
Posts: 4311
Posted: 02:05pm 22 Jan 2021
Copy link to clipboard 
Print this post

  jirsoft said  Or simpler:
Function reverse(in_string$) As string
  reverse = ""
  For i = Len(in_string$) To 1 Step -1
      CAT reverse, Mid$(in_string$,i,1)
  Next i
End Function


Simpler, but wrong

- 'reverse' doesn't need setting to "", that is the default on entry.
- 'i' is an undeclared global as Peter said.

Tom
MMBasic for Linux, Game*Mite, CMM2 Welcome Tape, Creaky old text adventures
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10310
Posted: 02:05pm 22 Jan 2021
Copy link to clipboard 
Print this post

Function reverse(in_string$) As string
  local r$
  local i
  For i = Len(in_string$) To 1 Step -1
      b$=Mid$(in_string$,i,1)
      r$=r$+b$
  Next i
  reverse=r$
End Function


For i = 1 To 100
  If Bin$(i) = reverse(Bin$(i)) Then
      If Hex$(i) = reverse(Hex$(i)) Then
          Print i,Bin$(i), Hex$(i)
      EndIf
  EndIf
Next i
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 02:34pm 22 Jan 2021
Copy link to clipboard 
Print this post

  matherp said  The first obvious bug is that i and r$ are global variables and are never re-initialised after the first time it is used. i in particular is buggered by the subroutine

USE OPTION EXPLICIT!!!!!!!!!


DOH!!!!
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 02:35pm 22 Jan 2021
Copy link to clipboard 
Print this post

  matherp said  
Function reverse(in_string$) As string
  local r$
  local i
  For i = Len(in_string$) To 1 Step -1
      b$=Mid$(in_string$,i,1)
      r$=r$+b$
  Next i
  reverse=r$
End Function


For i = 1 To 100
  If Bin$(i) = reverse(Bin$(i)) Then
      If Hex$(i) = reverse(Hex$(i)) Then
          Print i,Bin$(i), Hex$(i)
      EndIf
  EndIf
Next i


Thank you all for the comments and tweaks -- my own sloppy habits from Python krept in!!.

Nim
Entropy is not what it used to be
 
Nimue

Guru

Joined: 06/08/2020
Location: United Kingdom
Posts: 420
Posted: 12:24pm 23 Jan 2021
Copy link to clipboard 
Print this post

Thank you for your help on this the other day.

I use my CMM2 to indulge my hobby of working on number sequences:  https://oeis.org/A340559 << where you will see that I have added MM Basic solution to this sequence.

Cheers
Nim
Entropy is not what it used to be
 
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