![]() |
Forum Index : Microcontroller and PC projects : MM Basic - passing by reference help
Author | Message | ||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 420 |
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 KingdomPosts: 10310 |
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 KingdomPosts: 4311 |
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 RepublicPosts: 533 |
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 KingdomPosts: 4311 |
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 KingdomPosts: 10310 |
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 KingdomPosts: 420 |
DOH!!!! Entropy is not what it used to be |
||||
Nimue![]() Guru ![]() Joined: 06/08/2020 Location: United KingdomPosts: 420 |
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 KingdomPosts: 420 |
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 |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |