Author Topic: Error occurs with previously working Function  (Read 4802 times)

0 Members and 1 Guest are viewing this topic.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Error occurs with previously working Function
« on: October 05, 2021, 12:52:21 am »
This Binary function used to work just fine, until the latest dev update I installed today.

Code: [Select]
_Title "Test of BIN$ function"
Screen _NewImage(120, 43, 0)
Color 1, 7
Cls
1 Input "Enter decimal (0 exits) -> ", dec
If dec = 0 Then End
binvalue$ = Bin$(dec)
recdec = Val("&b" + binvalue$)
hexvalue$ = Hex$(dec)
Print "Decimal"; dec; "  equals binary "; binvalue$; "  equals hex "; hexvalue$; "  reconstructs to decimal"; recdec
Print
GoTo 1
End

'---------------------------------------------------------------------
'Function:  Convert any given dec/hex/oct number into a binary string.
'           Can handle positive and negative values and works in that
'           similar to the QB64 built-in HEX$ and OCT$ functions.
'
'Synopsis:  binary$ = BIN$ (value&&)
'
'Result:    binary$ --> the binary representation string of the given
'                       number without leading zeros for positive values
'                       and either 8/16/32 or 64 chars for negatives,
'                       depending on the input size
'
'Inputs:    value&& --> the pos./neg. number to convert, may also be
'                       given as &H or &O prefixed value
'
'Notes:     You may also pass in floating point values, as long as its
'           represented value fits into the _INTEGER64 (&&) input, hence
'           approx. -9.223372036854776E+18 to 9.223372036854776E+18.
'           Different from HEX$ and OCT$, BIN$ won't throw an overflow
'           error, if this range is exceeded, but the result is probably
'           wrong in such a case.
'---------------------------------------------------------------------
Function Bin$ (value&&)
    '--- option _explicit requirements ---
    Dim temp~&&, charPos%, highPos%
    '--- init ---
    temp~&& = value&&
    Bin$ = String$(64, "0"): charPos% = 64: highPos% = 64
    '--- convert ---
    Do
        If (temp~&& And 1) Then Mid$(Bin$, charPos%, 1) = "1": highPos% = charPos%
        charPos% = charPos% - 1: temp~&& = temp~&& \ 2
    Loop Until temp~&& = 0
    '--- adjust negative size ---
    If value&& < 0 Then
        If -value&& < &HFFFFFFFF~& Then highPos% = 33
        If -value&& < &H0000FFFF~& Then highPos% = 49
        If -value&& < &H000000FF~& Then highPos% = 57
        If -value&& < &H00000000~& Then highPos% = 1
    End If
    '--- set result ---
    Bin$ = Mid$(Bin$, highPos%)
End Function

Who was the helpful soul who wrote this? Bplus? I don't remember, but it wasn't Steve. Now I get this blocking error message, which refers to this line highlighted in red:

       If (temp~&& And 1) Then Mid$(Bin$, charPos%, 1) = "1": highPos% = charPos%

"Incorrect number of arguments passed to function on line 45.
Caused by (or after) MID$ ( BIN$, CHARPOS% , 1) = "1",1 "

Sorry, I don't know what that means, nor why suddenly there should be this fatal error? (I'm sure it's obvious to most decent programmers.)

Thanks!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Error occurs with previously working Function
« Reply #1 on: October 05, 2021, 01:15:08 am »
Recursive functions work now, so only use the function name when you're ready to assign it a return value.

For example, this will now error:

FUNCTION foo$ (a$)
   foo$ = _TRIM$(a$)
   foo$ = MID$(foo$, 4)
END FUNCTION

the 3rd line now sees the foo$ inside the MID$ as a recursive call back to itself and will error out....

Fix is:


FUNCTION foo$ (a$)
   temp$ = _TRIM$(a$)
   foo$ = MID$(temp$, 4)
END FUNCTION

*ONLY* use the function name inside the function to assign return values, unless you just want to recursively call the function again.

At this point, I wish we had a simple RETURN (value) command which we could tell folks to use, and then advise them to avoid function names inside a function completely.  After all, when you SUB foo, you don’t reference a variable named foo anywhere inside it.  ;D
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Error occurs with previously working Function
« Reply #2 on: October 05, 2021, 01:15:29 am »
Hi @Bert22306

Looks like function came from Utilities here:
https://www.qb64.org/forum/index.php?topic=1602.msg108167#msg108167

See if this mod works for you:
Code: QB64: [Select]
  1. _Title "Test of BIN$ function"
  2. Screen _NewImage(120, 43, 0)
  3. Color 1, 7
  4. 1 Input "Enter decimal (0 exits) -> ", dec
  5. If dec = 0 Then End
  6. binvalue$ = Bin$(dec)
  7. recdec = Val("&b" + binvalue$)
  8. hexvalue$ = Hex$(dec)
  9. Print "Decimal"; dec; "  equals binary "; binvalue$; "  equals hex "; hexvalue$; "  reconstructs to decimal"; recdec
  10.  
  11. '---------------------------------------------------------------------
  12. 'Function:  Convert any given dec/hex/oct number into a binary string.
  13. '           Can handle positive and negative values and works in that
  14. '           similar to the QB64 built-in HEX$ and OCT$ functions.
  15. '
  16. 'Synopsis:  binary$ = BIN$ (value&&)
  17. '
  18. 'Result:    binary$ --> the binary representation string of the given
  19. '                       number without leading zeros for positive values
  20. '                       and either 8/16/32 or 64 chars for negatives,
  21. '                       depending on the input size
  22. '
  23. 'Inputs:    value&& --> the pos./neg. number to convert, may also be
  24. '                       given as &H or &O prefixed value
  25. '
  26. 'Notes:     You may also pass in floating point values, as long as its
  27. '           represented value fits into the _INTEGER64 (&&) input, hence
  28. '           approx. -9.223372036854776E+18 to 9.223372036854776E+18.
  29. '           Different from HEX$ and OCT$, BIN$ won't throw an overflow
  30. '           error, if this range is exceeded, but the result is probably
  31. '           wrong in such a case.
  32. '---------------------------------------------------------------------
  33. Function Bin$ (value&&)
  34.     '--- option _explicit requirements ---
  35.     Dim temp~&&, charPos%, highPos%
  36.     '--- init ---
  37.     temp~&& = value&&
  38.     B$ = String$(64, "0"): charPos% = 64: highPos% = 64
  39.     '--- convert ---
  40.     Do
  41.         If (temp~&& And 1) Then Mid$(B$, charPos%, 1) = "1": highPos% = charPos%
  42.         charPos% = charPos% - 1: temp~&& = temp~&& \ 2
  43.     Loop Until temp~&& = 0
  44.     '--- adjust negative size ---
  45.     If value&& < 0 Then
  46.         If -value&& < &HFFFFFFFF~& Then highPos% = 33
  47.         If -value&& < &H0000FFFF~& Then highPos% = 49
  48.         If -value&& < &H000000FF~& Then highPos% = 57
  49.         If -value&& < &H00000000~& Then highPos% = 1
  50.     End If
  51.     '--- set result ---
  52.     Bin$ = Mid$(B$, highPos%)
  53.  
  54.  

Rho named a variable same, Bin$, same as Function name, Bin$, inside that function, not a good idea.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Error occurs with previously working Function
« Reply #3 on: October 05, 2021, 01:16:24 am »
Steve posted just as I finished my fix.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Error occurs with previously working Function
« Reply #4 on: October 05, 2021, 04:09:08 am »
Yep, that worked, however an official fix to it comes later today when I rollout all my updates regarding this recursion problem, however @bplus or @Qwerkey, you would be then responsible for updating the affected parts in the "Utilities" board.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Error occurs with previously working Function
« Reply #5 on: October 05, 2021, 05:31:54 am »
Hey, many thanks, guys!! Glad to have this working again. I've been using it quite a bit, and suddenly discovered several broken programs today. I didn't do it!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Error occurs with previously working Function
« Reply #6 on: October 05, 2021, 10:07:31 am »
Hi @Bert22306,

the new official Bin$ is out now https://www.qb64.org/forum/index.php?topic=4261, I strongly recomment you to update, because when I look on your provided code samples, then I see you still use my very old Bin$.

RhoSigma
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline George McGinn

  • Global Moderator
  • Forum Regular
  • Posts: 210
    • View Profile
    • Resume
Re: Error occurs with previously working Function
« Reply #7 on: October 05, 2021, 12:58:02 pm »
Well, I'm going crazy. Several things caused me to write new lyrics to "Careless Whispers."

Last night I woke up after watching Rugby on YouTube and low and behold a video done by @FellippeHeitor about subs and functions was playing, and he created an endless loop by calling a function within function. Then I read the post below. I lost it when I was playing a metal rock Genius playlist created by Apple while working on documentation and help screens for my application when Wham's Careless Whispers played. WTF - That song isn't like those done by Guns and Roses, AC/DC, Metallica, etc.

So I rewrote Careless Whispers  and called it "Careless Looping". Here are the first take on the lyrics. Have fun, enjoy, and I know others more creative will update the song better than I.

Quote
"Careless Looping"

I feel so unsure
As I write this code
And lead you to the dance floor
As my program dies
Something in my eyes
Calls to mind some crappy code
And all its sad loo-oop- ping

I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven

Time can never mend
The endless looping of a good function
To the heart and mind
Ignorance is kind
There's no comfort in the truth
Looping is all you'll find

I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am do-oo-ing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven

I'm never gonna code again
Guilty hands have got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven

Never without your help

Tonight the function must somehow stop
I wish that I could lose this loop
Maybe it's better this way
We'd hurt each other with the code we post online
We could have been so good together
We could have lived this run forever
But now who's gonna stop my code?
Please stop running

I'm never gonna code again
Guilty fingers got no reason
Though it's easy to pretend
I know what I am doing
I should've known better than to write a function
And waste the chance that I'd been given
So I'm never gonna code again
The way I did when I was eleven

Now that code won't end
Now that code won't end

Now that my code done
Was what it I did so wrong, so wrong
That you had to leave me alone?


Recursive functions work now, so only use the function name when you're ready to assign it a return value.

For example, this will now error:

FUNCTION foo$ (a$)
   foo$ = _TRIM$(a$)
   foo$ = MID$(foo$, 4)
END FUNCTION

the 3rd line now sees the foo$ inside the MID$ as a recursive call back to itself and will error out....

Fix is:


FUNCTION foo$ (a$)
   temp$ = _TRIM$(a$)
   foo$ = MID$(temp$, 4)
END FUNCTION

*ONLY* use the function name inside the function to assign return values, unless you just want to recursively call the function again.

At this point, I wish we had a simple RETURN (value) command which we could tell folks to use, and then advise them to avoid function names inside a function completely.  After all, when you SUB foo, you don’t reference a variable named foo anywhere inside it.  ;D
____________________________________________________________________
George McGinn
Theoretical/Applied Computer Scientist
Member: IEEE, IEEE Computer Society
Technical Council on Software Engineering
IEEE Standards Association
American Association for the Advancement of Science (AAAS)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Error occurs with previously working Function
« Reply #8 on: October 05, 2021, 01:21:55 pm »
LOL @Pete lookout, you've got competition!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Error occurs with previously working Function
« Reply #9 on: October 05, 2021, 02:52:11 pm »
I remember that one. It was written by an artist who submitted this to the rock group Genesis.

Got out of stack, wasn't feeling too good
With my laptop and compiler, a new browser too
The sun is shining so I head for the park,
With a cold pack of Red Bull, and a new pack of floppy disks

I got a cousin he works for MS,
Who thought that he knew a way he could help
At his apartment I knocked on the door,
He wouldn't come out until he got paid.
Now don't tell anybody that my program's toast
If they find out you know that they'll never let me post, cos

It's no ball getting an illegal function call
No, it's no ball getting an illegal function call

----------------------------------------------------------

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Error occurs with previously working Function
« Reply #10 on: October 05, 2021, 03:39:35 pm »
the new official Bin$ is out now https://www.qb64.org/forum/index.php?topic=4261, I strongly recomment you to update, because when I look on your provided code samples, then I see you still use my very old Bin$.

RhoSigma

Ah, so it was you. Yes, thanks, I updated the function to your latest version, and all works like a charm. Many thanks again, RhoSigma.

Offline Bert22306

  • Forum Regular
  • Posts: 206
    • View Profile
Re: Error occurs with previously working Function
« Reply #11 on: October 05, 2021, 03:45:37 pm »
Well, I'm going crazy. Several things caused me to write new lyrics to "Careless Whispers."

Last night I woke up after watching Rugby on YouTube and low and behold a video done by @FellippeHeitor about subs and functions was playing, and he created an endless loop by calling a function within function. Then I read the post below. I lost it ...

Maybe it was all part of the dream.

Like, just the other day, I thought I had awakened. I was going about my business, talking to my wife, even, and then I said wait! How is there this wall to wall carpeting in this corner of the room, when we changed the floor to hardwood?

Crazy stuff. It was recursive, but it didn't give me that error message. Probably it would have now, though, that I installed the new dev build.