Author Topic: Bug or design? - GOSUB related  (Read 3179 times)

0 Members and 1 Guest are viewing this topic.

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Bug or design? - GOSUB related
« on: December 09, 2018, 12:05:58 pm »
Just stumbled over this one, i know the shown concept is certainly not a nice programming style, but nevertheless i think a smart language should be able to handle it gracefully.

Code: QB64: [Select]
  1. GOSUB main
  2.  
  3. main:
  4. Test -1 '0 = working / -1 = causing error on next line
  5.  
  6. SUB Test (baam%)
  7.     GOSUB Test_sub
  8.     EXIT SUB
  9.  
  10.     Test_sub:
  11.     IF baam% THEN EXIT SUB '<-- this EXIT SUB should discard all pending return points of SUB Test()
  12.     RETURN '                    to avoid errors in the GOSUB/RETURN hirarchie
  13.  
  14.  
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 RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Bug or design? - GOSUB related
« Reply #1 on: December 09, 2018, 12:14:00 pm »
Here is another one, which doesn't really makes sense to me.

Code: QB64: [Select]
  1. GOSUB main
  2.  
  3. main:
  4. CLEAR '<-- destroys GOSUB/RETURN hirarchie
  5.  
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 SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Bug or design? - GOSUB related
« Reply #2 on: December 09, 2018, 12:17:17 pm »
Personally, I’m of the opinion that QB64 subs are slow enough already.  Do we really need to add more overhead to them as well, to increase their delay?

SUB test (x)
   GOSUB test_x
   IF x THEN EXIT SUB ‘exit condition after return clears gosub stack
   EXIT SUB


  test_x:
  IF x THEN RETURN
  RETURN
END SUB


BASIC has always had issues with GOSUBS and exiting them without using RETURN.  Honestly, I’d suggest to just make and call another SUB instead.
« Last Edit: December 09, 2018, 12:18:41 pm by odin »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

FellippeHeitor

  • Guest
Re: Bug or design? - GOSUB related
« Reply #3 on: December 09, 2018, 12:20:54 pm »
Here is another one, which doesn't really makes sense to me.

Code: QB64: [Select]
  1. GOSUB main
  2.  
  3. main:
  4. CLEAR '<-- destroys GOSUB/RETURN hirarchie
  5.  

It all resembles malpractice after all. One should be aware of their stack if they choose to use GOSUB.

CLEAR is also doing its job as expected here, clearing it all as well as the stack.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Bug or design? - EXIT SUB/FUNC in local GOSUB routines
« Reply #4 on: December 09, 2018, 12:22:26 pm »
In recall in QBasic this type of programming practice (Exiting a gosub without a return) would likely cause a stack space overflow error. Using GOTO statements in GOSUB statements were usually found to be the main culprit.  Memory was very limited then in regards to what it is in QB64. I actually don't know if EXIT SUB in QB64 will return the stack or not. In Atari, POP was used if you wanted to exit a gosub without completing a return. Using POP unloaded the GOSUB call from the memory stack.

Ah three other posts while I was writing this, well..

In regard to CLEAR, I believe QBasic had some restrictions on where the CLEAR statement could and could not be placed. CLEAR and RUN are both a bit buggy in QB64.

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