QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Raptor88 on April 16, 2019, 07:56:02 pm

Title: GOSUB VS SUB
Post by: Raptor88 on April 16, 2019, 07:56:02 pm
When should one use a (GOSUB + RETURN) vs using (SUB + END SUB)?
Title: Re: GOSUB VS SUB
Post by: FellippeHeitor on April 16, 2019, 08:30:54 pm
Both serve the same purpose but SUB is more versatile.
Title: Re: GOSUB VS SUB
Post by: FellippeHeitor on April 16, 2019, 08:31:35 pm
And easier to maintain. But there's no rule and both are valid strategies for code reusing.
Title: Re: GOSUB VS SUB
Post by: FellippeHeitor on April 16, 2019, 08:32:32 pm
In summary, it's up to you.
Title: Re: GOSUB VS SUB
Post by: bplus on April 16, 2019, 08:54:37 pm
You should use SUB ... END SUB always for
1. recursive subs
2. variable isolation from main code and other subs
3. passing arguments
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 16, 2019, 09:09:27 pm
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 16, 2019, 10:01:58 pm
In summary, it's up to you.
Thanks for your input.
Raptor88
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 16, 2019, 10:10:33 pm
You should use SUB ... END SUB always for
1. recursive subs
2. variable isolation from main code and other subs
3. passing arguments
1. recursive subs
.... beyond my scope for now, but good to know.

2. variable isolation from main code and other subs
.... This I don't understand.  When I pass a variable as an argument, changes made to the variable within the sub changes the original variable.  I read that this is by design.

3. passing arguments
.... Yes, I haven't seen how to pass arguments using GOSUB, so SUB would be the way to do that.  Good point.

Thanks for the help,
Raptor88
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 16, 2019, 10:13:35 pm
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

Thank you for the history lesson.  I didn't know any of what you just explained.  I will use SUB + END SUB as you recommend since that is the more modern method.
Raptor88
Title: Re: GOSUB VS SUB
Post by: bplus on April 16, 2019, 11:24:40 pm
Quote
2. variable isolation from main code and other subs
.... This I don't understand.  When I pass a variable as an argument, changes made to the variable within the sub changes the original variable.  I read that this is by design.

Yes this is true about the variable arguments passed to the sub but all the variables created inside the sub are erased when the sub finishes so if you use an i in the sub, it won't effect the i in the main code as it would with GOSUB.

Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 17, 2019, 12:27:36 am
Yes this is true about the variable arguments passed to the sub but all the variables created inside the sub are erased when the sub finishes so if you use an i in the sub, it won't effect the i in the main code as it would with GOSUB.

Ahhh, now the light bulb has turned on.  Thanks for taking the time to explain this valuable info.
Raptor88
Title: Re: GOSUB VS SUB
Post by: bplus on April 17, 2019, 08:05:18 am
Hi Raptor88,

Here is an example of a recursive sub. It calls itself over and over again until the job is done.

IMHO it is the most beautiful and elegant kind of code you can write. I hope this fractal inspires:
Code: QB64: [Select]
  1. _TITLE "recurring squares 2017-10-26 by bplus"
  2. ' Now with Alpha coloring!
  3. 'reoccuring squares SmallBASIC translation from
  4. REM reoccuring squares NaaLaa started 2015-05-14 MGA/B+
  5.  
  6. CONST xmax = 700
  7. CONST ymax = 700
  8.  
  9. SCREEN _NEWIMAGE(xmax, ymax, 32)
  10. _SCREENMOVE 360, 30 'adjust as needed _MIDDLE needs a delay .5 or more for me
  11. COMMON SHARED dimmer
  12. sq = 700: dir = 1
  13.     CLS
  14.     white& = _RGB(255, 255, 255)
  15.     fRecStep 0, 0, sq, sq, white&
  16.     sqPlus sq / 2, sq / 2, sq / 2
  17.     _DISPLAY
  18.     _LIMIT 30
  19.     dimmer = dimmer + dir
  20.     IF dimmer > 255 THEN dimmer = 255: dir = dir * -1: _DELAY .5
  21.     IF dimmer < 0 THEN dimmer = 0: dir = dir * -1: _DELAY .5
  22.  
  23. SUB fRecStep (x1, y1, x2, y2, c&)
  24.     LINE (x1, y1)-STEP(x2, y2), c&, BF
  25.  
  26. SUB sqPlus (x, y, side)
  27.     cx = x - side / 2: cy = y - side / 2
  28.     fRecStep cx, cy, side, side, _RGBA(0, 0, 0, dimmer)
  29.     IF side < 10 THEN EXIT SUB
  30.     ns = side / 2: nc = colorNumber - 35
  31.     sqPlus cx, cy, ns
  32.     sqPlus cx + side, cy, ns
  33.     sqPlus cx, cy + side, ns
  34.     sqPlus cx + side, cy + side, ns
  35.  
  36.  
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 17, 2019, 12:09:32 pm
To further add to what I said:

In early versions of BASIC there was no variable scope.  All variables were global.  Line numbers were required (10, 20, 30, 40, etc.) and GOSUB jumped to a line number.  QBASIC got rid of the requirement for line numbers, and added optional line labels (labels behave like line numbers, in global scope, but have an easier to remember name rather than a number).  I can still remember the first time I used QBASIC after years of Commodore 64 BASIC and DOS GW-BASIC, it was so weird not needing line numbers!  I could barely believe it.  And an IDE that showed you your whole program!  In GW-BASIC you were left going LIST to display your lines of code, and if it was more than a screen of lines, you'd be forced to work out a range to display, like LIST 150-180.

Code: QB64: [Select]
  1. 10 CLS
  2. 20 INPUT "Enter number 1: ", Num1!
  3. 30 INPUT "Enter number 2: ", Num2!
  4. 30 PRINT Num1!; "+"; Num2!; "="; (Num1! + Num2!)
  5. 40 GOTO 20

People usually went by 10s for line numbers, that way you could add up to 9 more lines in-between existing lines without being forced to renumber existing lines to make room.  For example, in the above addition app, if I wanted to print a blank line after the answer, I could do that as:

Code: QB64: [Select]

QBASIC with no line number requirements was a breath of fresh air!  It made coding MUCH simpler and easier, especially when making changes then testing, changes then testing... in GW-BASIC you had to go by 100s if you wanted to make sure you would have enough space to add groups of lines later, or else forced to use GOTO spaghetti code to jump around, which was an absolute nightmare for code readibility!

GOSUB came from that time period.  It was better than GOTO, because you returned to where you started after, but it was still confusing keeping track of all the subroutines splashed around the code.  I would often add huge line numbers for my subs so they stayed at the bottom, such as starting my subs at line 8000.

SUB + END SUB was an amazing game changer when it came out.  No strict line numbers, neatly grouped together at the end of your code, accessible within the IDE with F2 including pressing F2 on a subroutine's name, new FUNCTION ability with a data type (e.g. MyStringFunc$ returns a string).

The only time to use GOSUB in a modern BASIC would be quick-and-dirty proof-of-concept programming.  Hacking something together where the only goal is getting it done fast.  For the most manageable code that is easiest to read and quick to update, definitely stick to SUB + END SUB / FUNCTION + END FUNCTION.  In my opinion.
Title: Re: GOSUB VS SUB
Post by: Qwerkey on April 17, 2019, 12:51:54 pm
The only time to use GOSUB in a modern BASIC would be quick-and-dirty proof-of-concept programming.

I wouldn't even bother using it then.  Just pretend that GOSUB has never existed.
Title: Re: GOSUB VS SUB
Post by: MWheatley on April 17, 2019, 01:11:11 pm
Just to add:

GOSUB + RETURN was the original BASIC method for subroutines, such as what you would see in GW-BASIC.  SUB + END SUB was a new feature added to QBASIC, and supported by a few other BASIC variants.

I personally recommend using SUB + END SUB, as it can do more, and do it simpler.  As far as I know, GOSUB labels do not show up under F2 to list subs/functions, which is a really great feature of SUB + END SUB.

I think you're right about F2.  I run a lot of legacy code, using GOSUB, and only recently created a new program with a SUB/ END SUB construction, because it was necessary to pass a variable.

The SUB/END SUB showed up in edit mode, and I couldn't remember how to get back to see the main program listing.  What are you supposed to do?

Malcolm

(PS: I was running it in QB45, not QB64, on this occasion, as it was being run on a Linux platform.)
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 17, 2019, 01:17:24 pm
I'm not quite sure what you're asking about F2.
Title: Re: GOSUB VS SUB
Post by: RhoSigma on April 17, 2019, 03:01:20 pm
Hi, just wanna give my two cents on this topic :)

1.) I use GOSUB/RETURN in most cases when it comes to reduce the  EXE's size by pulling together code sequences which are redundant in several parts of my project. If the affected code don't need passing any arguments or relies on global variables only, this is a quick way to put the redundant code into a GOSUB routine and just calling it from any place I need it. (eg. the GuiToolsDemo.bas of my GuiTools project https://www.qb64.org/forum/index.php?topic=809.msg100182#msg100182)

2.) To create some kind of black box modules, hence just show the main functions of a library under F2, but hide the sub-code or rendundancies within the main functions. This i will usually do with some pseudo argument passing, giving the GOSUB routine arguments/result a prefix like the initials of the GOSUB routine and assigning values to it before actually calling the GOSUB routine, and later get back the result the same way. (currently only done that way in my LZW-Packer library https://www.qb64.org/forum/index.php?topic=783.0)

In general, treating GOSUB/RETURN as it never would have exist is rude in my opinion, you just need to know what you wanna achive in your program using your personal programming style. Important is, whatever way you choose to use, be consistent on it throughout your projects, avoid to mix different styles of doing things, it will help YOU understanding things you did, when looking at it after a year and it will also help other users to understand/adapt to your style, when you publish code snippits here in the forum.
Title: Re: GOSUB VS SUB
Post by: FellippeHeitor on April 17, 2019, 03:37:43 pm
Truth be told, I even have SUBs in my code that have GOSUB in them. Judge me. :-)
Title: Re: GOSUB VS SUB
Post by: jack on April 17, 2019, 03:55:52 pm
in my opinion, gosub's are ok, but the problem is that it can lead to some very hard (or impossible) to decipher spaghetti code.
if people confine the subroutine code in clearly defined sections, then subroutines are ok.
Title: Re: GOSUB VS SUB
Post by: RhoSigma on April 17, 2019, 05:34:12 pm
...
in my opinion, gosub's are ok, but the problem is that it can lead to some very hard (or impossible) to decipher spaghetti code.
...

I would agree with you 25- 30 years ago, today spaghettti code is rather a phenomenon caused by people (out of that era), which played around a little bit with programming in the ancient times and comming back to it todays, (maybe because they got retired and feeling bored now or for other reasons). Those people simply "re-enter" programming today with the attitude which was usual for that ancient time (mostly because they find old code snippits on a floppy disk, and they don't know how to do it better). Instead of doing the right thing, learning what has changed in the past 25-30 years first, they simply stumble into the modern programming world with their old stuff.

In short i think, spaghetti code is a matter of insufficient programming skills (at least seen to the rules and methods applicable todays), and whenever those people appear here on the forum, we should kindly try to help and move those fellows into the right direction. Spaghetti code is not the fault of GOSUB/RETURN or GOTO, it's simply insufficient knowledge how to do it right.
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 17, 2019, 06:45:10 pm
"Spaghetti code is not the fault of GOSUB/RETURN or GOTO, it's simply insufficient knowledge how to do it right."

I can tell you, spaghetti code was almost guaranteed with C=64 or GW-BASIC code.  You ran out of line numbers in one part, then used GOTO because renumbering 200 lines of code (and all references to those lines of code) is no fun at all.  The more complex the program got, the worse the spaghetti code became.  I started mapping out the line numbers in the thousands for the various parts of my programs, just so I never had to renumber anything.  It did look silly on small programs, mind you, lol.

The early BASIC spaghetti code was directly the fault of the BASIC editor (or lack thereof).  Since Q-BASIC, there has been no excuse for using GOTO or other code kludges.  It's been all IDEs since then, probably because of how ubiquitous QBASIC became for new programmers looking to try programming.  Even QuickBASIC v4.5 was nowhere near as advanced as say Turbo Pascal (and nowhere near as fast), but for "included with the operating system" it was truly amazing!  Light years beyond C=64 or GW-BASIC.  I think I've only spent a few hours programming in BASICs that require line numbers since that first time I loaded Q-BASIC.

Personally, I find more modern IDEs to be mostly garbage.  QuickBASIC v4.5 was my dream programming environment as a kid, built-in help allowed me to learn [almost] the whole language without opening a book.  I find a lot of modern programming IDEs have too much clutter and garbage that gets in the way of actually focusing on programming, and can have performance issues on larger projects.  I loved DOS 16-colour text mode GUIs, like PC-Tools and the Norton suite.  Very efficient, everything can be done with a couple key clicks.  Modern GUIs often require you to use the mouse, or jump through hoops with the keyboard -- why should I have to press 7 keys to do a basic function?  I feel mouse-oriented apps often forget about user efficiency altogether -- that is, actually getting the task done in the shortest period of time.

I have been enjoying getting back into programming these past months, in a modern programming language that also respects the old-school Q-BASIC/QuickBASIC efficient IDE style.  It also seems to compile quickly.  ~2,700 lines of code in ~3 seconds... not sure how it would perform with 50,000 lines of code.  Back in the day, QuickBASIC v4.5 took about that long to launch your app on a 286 when you hit F5, and significantly longer if you wanted to produce a standalone .EXE file.

In the modern era, spaghetti code usually refers to programmers that don't know how to program themselves, but are okay at reading other people's code.  They do their programming jobs by doing web searches for everything they're trying to accomplish, then gluing the pieces they find together until they mostly work.  Nobody will be able to tell at first glance what this code does, as there is zero structure present.  But the original spaghetti code was definitely GOTO.  What an ugly nightmare it was with forced line numbers and GOTO.  There were literally magazines with terrible BASIC code, sometimes having a dozen GOTO statements.  I think a few of the programmers reveled in writing ugly obfuscated code, making their programs seem more ineffably impressive to the general public.

Q-BASIC might be one of the greatest contributions to computing by Microsoft ever.  Did they actually produce the IDE themselves?  Or just buy up a preexisting software package.  I've never looked into the history of the IDE itself, I was just an enthusiastic user at the time.  Pretty sure QuickBASIC v4.5 was my favourite app on my 286, although I also loved speed-running Wolfenstein 3D using a mouse + keyboard (everyone else had Doom on their 486s, I had Wolf3D on a 286).
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 17, 2019, 09:57:59 pm
Hi Raptor88,

Here is an example of a recursive sub. It calls itself over and over again until the job is done.

IMHO it is the most beautiful and elegant kind of code you can write. I hope this fractal inspires:
.... snip to save bandwidth ....

Nice screensaver.  As previously mentioned, recursive subs are beyond my scope for now but it's informative to see a working example.
Thanks,
Raptor88
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 17, 2019, 09:59:44 pm
To further add to what I said:
.... snip to save bandwidth ....

Interesting read.
Thanks,
Raptor88
Title: Re: GOSUB VS SUB
Post by: Jack002 on April 17, 2019, 11:06:36 pm
No, we still need gosub today. The concept of a program is to do some big job (I'm waving an arm left to right) and to do that you split it into smaller and smaller jobs. Many you need to do more than once. So write a function or a subroutine.
The first all encompassing gosub anyone will ever need is the "get ready". You call it to set up things for the main process.
Later you might need a "finish up". Close files, tidy things somehow.
If you don't take a large plan and break it into smaller ones you'll never get a program off the ground.

Any program worth looking at will have subroutines. The work is ordered and structured by them. One may make a menu, one loads files, one saves files, one formats data, one gets inputs to add to something, one makes a box on the screen, etc etc etc.

So you mention recursion earlier. I love that. Lots of things can be done with it, but you have to do it right. You'll kill your stack if it gets away from you. Ever see a recursive sort? They are wonderful. I did some recursion, not sure what they were now.
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 17, 2019, 11:09:27 pm
No, we still need gosub today. The concept of a program is to do some big job (I'm waving an arm left to right) and to do that you split it into smaller and smaller jobs. Many you need to do more than once. So write a function or a subroutine.
The first all encompassing gosub anyone will ever need is the "get ready". You call it to set up things for the main process.
Later you might need a "finish up". Close files, tidy things somehow.
If you don't take a large plan and break it into smaller ones you'll never get a program off the ground.

Any program worth looking at will have subroutines. The work is ordered and structured by them. One may make a menu, one loads files, one saves files, one formats data, one gets inputs to add to something, one makes a box on the screen, etc etc etc.

So you mention recursion earlier. I love that. Lots of things can be done with it, but you have to do it right. You'll kill your stack if it gets away from you. Ever see a recursive sort? They are wonderful. I did some recursion, not sure what they were now.

Nobody was suggesting that subroutines are not useful anymore, just that the GOSUB statement is less robust than the SUB + END SUB method.
Title: Re: GOSUB VS SUB
Post by: freetrav on April 18, 2019, 08:37:10 am
"Spaghetti code is not the fault of GOSUB/RETURN or GOTO, it's simply insufficient knowledge how to do it right."

I can tell you, spaghetti code was almost guaranteed with C=64 or GW-BASIC code.  You ran out of line numbers in one part, then used GOTO because renumbering 200 lines of code (and all references to those lines of code) is no fun at all.  The more complex the program got, the worse the spaghetti code became.  I started mapping out the line numbers in the thousands for the various parts of my programs, just so I never had to renumber anything.  It did look silly on small programs, mind you, lol.

In GW-BASIC (I can't speak for C64 BASIC), you didn't have to hack around with changing line numbers or GOTO/GOSUBing to random code blocks inserted somewhere when you "ran out" of numbers; all you had to do was RENUM your program - you could even renumber sections of your program; see https://www-user.tu-chemnitz.de/~heha/viewchm.php/hs/gwbasic.chm/RENUM.html

Quote
The early BASIC spaghetti code was directly the fault of the BASIC editor (or lack thereof).  Since Q-BASIC, there has been no excuse for using GOTO or other code kludges.  It's been all IDEs since then, probably because of how ubiquitous QBASIC became for new programmers looking to try programming.  Even QuickBASIC v4.5 was nowhere near as advanced as say Turbo Pascal (and nowhere near as fast), but for "included with the operating system" it was truly amazing!  Light years beyond C=64 or GW-BASIC.  I think I've only spent a few hours programming in BASICs that require line numbers since that first time I loaded Q-BASIC.

The early BASIC spaghetti code was directly the fault of the PROGRAMMER, not the TOOL - if you didn't plan out your code, you would end up with spaghetti code. Certainly, BASIC as a language didn't encourage planning (or other good habits), the way languages such as FORTRAN, Pascal, or ALGOL - or even COBOL - did, but if you developed the good habits, you could still use them in BASIC and get good results (I speak from direct personal experience; I started with AppleSoft BASIC and Apple Pascal).

Quote
Personally, I find more modern IDEs to be mostly garbage.  QuickBASIC v4.5 was my dream programming environment as a kid, built-in help allowed me to learn [almost] the whole language without opening a book.  I find a lot of modern programming IDEs have too much clutter and garbage that gets in the way of actually focusing on programming, and can have performance issues on larger projects.  I loved DOS 16-colour text mode GUIs, like PC-Tools and the Norton suite.  Very efficient, everything can be done with a couple key clicks.  Modern GUIs often require you to use the mouse, or jump through hoops with the keyboard -- why should I have to press 7 keys to do a basic function?  I feel mouse-oriented apps often forget about user efficiency altogether -- that is, actually getting the task done in the shortest period of time.

I tend to consider most modern IDEs to be not garbage, but simply overkill for the kind of programming I do. I agree that the QBASIC/QB64 IDE is nearly ideal; I use Microsoft's Powershell ISE for working in Windows PowerShell, and Notepad++ for most other languages. On the other hand, I'm not busy writing large projects with several myriad lines of code; I tend to write small programs with focussed purposes, rarely with display-oriented presentation - following the UNIX philosophy of 'do one thing, do it well, and chain lots of do-one-thingers together to do more complex jobs'.

Title: Re: GOSUB VS SUB
Post by: bplus on April 18, 2019, 09:34:57 am
So a good use of GOSUB is when you find you are writing redundant code snips in the main program or loop or in a SUB or FUNCTION and you want to keep the variables "global" in that same level of code.
Title: Re: GOSUB VS SUB
Post by: MWheatley on April 18, 2019, 09:36:35 am
I'm not quite sure what you're asking about F2.

I fiddled with it, so that I could articulate the problem more precisely.  And in doing so, found the answer that had previously eluded me!  So, problem solved....

Malcolm
Title: Re: GOSUB VS SUB
Post by: Jack002 on April 18, 2019, 10:11:58 am
Nobody was suggesting that subroutines are not useful anymore, just that the GOSUB statement is less robust than the SUB + END SUB method.
Qwerkey  was:
I wouldn't even bother using it then.  Just pretend that GOSUB has never existed.
Title: Re: GOSUB VS SUB
Post by: Pete on April 18, 2019, 12:57:45 pm
It's like saying who's the greatest super hero, Batman or Superman? If you don't know the one correct answer, it's because you're too much of a woose to stand up for your own damn opinion! Sorry, Qwerty, Catwoman's just eye-candy.

Like Fell, I too include GOSUB in subs; so like he said, judge him.

I like to keep my libraries SUB free, and only use GOSUB. That way, when I put libraries into other programs, I don't have to sort through subs that are not part of my new creation. Still, I could prefix name all of the subs, so at least it wouldn't be messy when I search them.

SUBs are great for more variable control, otherwise using GOSUB is great for times you just have two or more repeating routines in your code, and you just want one routine to handle them.

Pete
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 18, 2019, 02:25:58 pm
Hi guys
GOSUB vs SUB.... mmmmh you loose FUNCTIONs!
:-)
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 18, 2019, 02:50:19 pm
Sorry I loose the point of discussion...

Pro               Contra
GOSUB            SUB Name (parameters)
                          CONST costants
                          DIM/SHARED global variables
                          DIM/REDIM Local_variables (STATIC)
                       
Label:                 .............
...                       EXIT SUB
...                       .............
RETURN            END SUB


Well in my little experience, I started to program in Qbasic/QB so I have no practice of GWBASIC and C-64 BASIC but only a referred terrorism versus GOTO and its child GOSUB!  Nevetheless reading of old BASIC there was a massive use of GOTO... but I don't know if that BASIC had also GOSUB, SELECT CASE, DO LOOP UNTIL, DO WHILE LOOP, IF THEN ELSE nested...., moreover I read that Pascal rose up to win the massive use of GOTO, but in its keywords there is the same GOTO :-)
In my practice I started using SUB and FUNCTION and when I went near the concept of flow event driven I approached to GOSUB by that Qbasic manages events like trap error, stick or pen input, music loop keyboard and timer events... and so on as all you still know.


surely I scare with the massive use of global variables... but also using SUB the parameters work like global variables that are changed into the SUB until if you pass parameter by value (not valid for string or typedef) or you use a local duplicate into the SUB. Moreover if you use an huge number of parameters it is very difficult to write code clear.
Nevertheless I like to use SUB versus GOSUB because i like to think to chunch the work into little works and so each task is performed by a block ... like the brick of lego... this is my habit but I can agree that with a solid habit I can get block of code working on its own also with GOSUB.
Thanks to read
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 18, 2019, 02:55:34 pm
Quote
It's like saying who's the greatest super hero, Batman or Superman?

@Pete
! do you forget Electra, DerilDevil and Flash, SuperGirl?
O_o
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 18, 2019, 03:04:31 pm
In GW-BASIC (I can't speak for C64 BASIC), you didn't have to hack around with changing line numbers or GOTO/GOSUBing to random code blocks inserted somewhere when you "ran out" of numbers; all you had to do was RENUM your program - you could even renumber sections of your program; see https://www-user.tu-chemnitz.de/~heha/viewchm.php/hs/gwbasic.chm/RENUM.html


Well, if I had known about GW-BASIC "RENUM," I could have saved myself hours of hardship!  The books/magazines I read about BASIC did not mention this command.  Too bad.

I wonder how many other early BASIC programmers knew about RENUM?
Title: Re: GOSUB VS SUB
Post by: freetrav on April 19, 2019, 08:44:48 am
Nevetheless reading of old BASIC there was a massive use of GOTO... but I don't know if that BASIC had also GOSUB, SELECT CASE, DO LOOP UNTIL, DO WHILE LOOP, IF THEN ELSE nested...., moreover I read that Pascal rose up to win the massive use of GOTO, but in its keywords there is the same GOTO :-)

GW-BASIC and similar didn't have SELECT CASE or DO UNTIL, but some did have WHILE...WEND (GW definitely did, but AppleSoft, for example, didn't), which was functionally equivalent to DO WHILE. IF...THEN was common; IF...THEN...ELSE was not - but it did exist.

The nearest thing to SELECT CASE was ON...GOTO/ON...GOSUB, which was very limited by comparison - e.g., ON X GOSUB 100, 200, 300 would only work if X had an integer value of 1, 2, or 3; if X=1 it would GOSUB 100; if X=2 it would GOSUB 200; if X=3 it would GOSUB 300, otherwise it would just fall through to the next line.  Minimally adequate, but no more than that.

Pascal's GOTO was very restricted compared to BASIC's; not only did you have to declare the label before using it, but you couldn't use GOTO to break out of a block structure. In reality, that was enough to make GOTO almost useless in Pascal, and I don't think I ever saw it actually used in a program that wasn't a contrived textbook example specifically to show how the statement worked.

Title: Re: GOSUB VS SUB
Post by: freetrav on April 19, 2019, 08:46:00 am

Well, if I had known about GW-BASIC "RENUM," I could have saved myself hours of hardship!  The books/magazines I read about BASIC did not mention this command.  Too bad.

I wonder how many other early BASIC programmers knew about RENUM?

Not intended to be snarky, but ... any of them that really READ the BASIC manual.
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 19, 2019, 09:53:53 am
Not intended to be snarky, but ... any of them that really READ the BASIC manual.

I started programming C=64 BASIC when I was 9 years old, GW-BASIC a couple years later.  Nobody I knew in that era knew about RENUM, but we were just kids experimenting.  I got into C=64 BASIC because I saw someone use POKE statements to make the screen flash random colours and scroll text up the screen.  It was the most awesome tech novelty I had ever seen, so I promptly memorised the commands he used.

I did have a GW-BASIC book at that time which I read through, but I don't remember seeing RENUM in it.
Title: Re: GOSUB VS SUB
Post by: SMcNeill on April 19, 2019, 10:14:49 am
C64 BASIC didn’t have  RENUM command; it wasn’t added until the last version of C128 BASIC (floppy disk version, I think — maybe some old cartridges). 

GW-BASIC had RENUM, but I never saw anyone use it seriously.  It was rather silly the way it worked.

RENUM 1000, 900, 20 —- This was a valid command that made no real sense without reading and rereading the documentation over and over....

RENUMber starting with 1000, any number >= 900, and count by 20s...

Everybody got the first and second values screwed up, and RENUM killed code more than it fixed it.  Everybody I know, who programmed back in the day, simply used GOTO/GOSUB to scrunch new “blocks” out to insert new code — thus leading to spaghetti code, as we call it now.
Title: Re: GOSUB VS SUB
Post by: MWheatley on April 19, 2019, 04:48:17 pm
C64 BASIC didn’t have  RENUM command; it wasn’t added until the last version of C128 BASIC (floppy disk version, I think — maybe some old cartridges). 

GW-BASIC had RENUM, but I never saw anyone use it seriously.  It was rather silly the way it worked.

RENUM 1000, 900, 20 —- This was a valid command that made no real sense without reading and rereading the documentation over and over....

RENUMber starting with 1000, any number >= 900, and count by 20s...

Everybody got the first and second values screwed up, and RENUM killed code more than it fixed it.  Everybody I know, who programmed back in the day, simply used GOTO/GOSUB to scrunch new “blocks” out to insert new code — thus leading to spaghetti code, as we call it now.

Yes.  I never trusted RENUM.  And parts of my PhD were written in BASIC, so that lack of trust mattered.

Malcolm
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 19, 2019, 06:41:43 pm
here https://www.qb64.org/forum/index.php?topic=1287.0 (https://www.qb64.org/forum/index.php?topic=1287.0) an example of mine to show a slight difference in the number of variables and in the way to manage data between SUB vs GOSUB
Title: Re: GOSUB VS SUB
Post by: Jack002 on April 19, 2019, 09:37:08 pm
*edit* never mind. Reply to a less than last post
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 20, 2019, 04:58:32 am
While programming, I discovered a reason why GOSUB should be used.

Code: QB64: [Select]
  1. x = 100
  2. GOSUB PrintStats
  3. CALL PrintStats2
  4. '========== SUBROUTINES ==========
  5. PrintStats:
  6. PRINT "The value of x is: "; x
  7.  
  8. SUB PrintStats2
  9.     PRINT "The value of x is: "; x

The print was:
The value of x is: 100  <== GOSUB printed the value
The value of x is: 0   <=== SUB did not print the value

I wanted a subroutine to print the values of 6 variables (statistics) at various points for troubleshooting.
Using SUB, the values were always zero, since I did not pass all of the variables to the SUB as arguments.
Using GOSUB, all of the values were printed without having to pass all of the variables as arguments.

It would be too unweildy to have to type 6 (or more) variables as arguments every time I inserted a CALL to the SUB.

So GOSUB should be used to print the contents of variables that have not been declared as SHARED.

Raptor88
Title: Re: GOSUB VS SUB
Post by: bplus on April 20, 2019, 09:12:31 am
Hi Raptor88,

A simple fix to your print SUB, giving it a parameter to pass it an argument to PRINT:
Code: QB64: [Select]
  1. FOR x = 1 TO 10
  2.     GOSUB PrintStats
  3.     'CALL PrintStats2(x) '<<< yuck to CALL and ()'s
  4.    PrintStats2 x
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2 (printThisNumber) '<<< Fixed with a parameter to pass PRINT and argument to PRINT
  11.     PRINT "From PrintStats2: The value of x is: "; printThisNumber
  12.  
  13.  

PS I don't usually use CALL so I don't have to put arguments inside ()'s

Everything inside a SUB is completely ignorant of variables and values from another part of program. It only knows what you pass it from outside or what is DIM SHARED in main code.

So another way to do this:
Code: QB64: [Select]
  1. DIM SHARED x AS INTEGER '<<< if you share x, you don't need to pass PrintStats2 an argument
  2. FOR x = 1 TO 10
  3.     GOSUB PrintStats
  4.     PrintStats2
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2
  11.     PRINT "From PrintStats2: The value of x is: "; x
  12.  
  13.  
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 20, 2019, 04:51:06 pm
Hi bplus,

Yes, both of the examples you posted are solutions.
For solution1, I didn't want to have to type 6 arguments for each CALL.
For solution2, I thought not making all of my variables global using SHARED was the way to go.
I'm using CALL for now as a novice, to make it obvious to me it is a call to a SUB.  I may use your method later for less typing.

Thanks for taking the time to help.  I do appreciate it,
Raptor88

Hi Raptor88,

A simple fix to your print SUB, giving it a parameter to pass it an argument to PRINT:
Code: QB64: [Select]
  1. FOR x = 1 TO 10
  2.     GOSUB PrintStats
  3.     'CALL PrintStats2(x) '<<< yuck to CALL and ()'s
  4.    PrintStats2 x
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2 (printThisNumber) '<<< Fixed with a parameter to pass PRINT and argument to PRINT
  11.     PRINT "From PrintStats2: The value of x is: "; printThisNumber
  12.  
  13.  

PS I don't usually use CALL so I don't have to put arguments inside ()'s

Everything inside a SUB is completely ignorant of variables and values from another part of program. It only knows what you pass it from outside or what is DIM SHARED in main code.

So another way to do this:
Code: QB64: [Select]
  1. DIM SHARED x AS INTEGER '<<< if you share x, you don't need to pass PrintStats2 an argument
  2. FOR x = 1 TO 10
  3.     GOSUB PrintStats
  4.     PrintStats2
  5.  
  6. '========== SUBROUTINES ==========
  7. PrintStats:
  8. PRINT "From GOSUB PrintStats: The value of x is: "; x
  9.  
  10. SUB PrintStats2
  11.     PRINT "From PrintStats2: The value of x is: "; x
  12.  
  13.  
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 20, 2019, 05:26:04 pm
Hi Raptor88

I agree with you that each one takes own method to code.

I don't agree with this
Quote
For solution2, I thought not making all of my variables global using SHARED was the way to go.
because with GOSUB all your variables are already in the main and are Global!
So with or without SHARED you use global variables.
A different kind of issue is that rised up by Fellippe
you can make a GOSUB inside a SUB or FUNCTION,

 but in this case the variables used in the GOSUB are those Global with SHARED into the main and those local of the SUB where GOSUB is.
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 20, 2019, 09:02:03 pm
Hi Raptor88

I agree with you that each one takes own method to code.

I don't agree with this because with GOSUB all your variables are already in the main and are Global!
So with or without SHARED you use global variables.
A different kind of issue is that rised up by Fellippe
you can make a GOSUB inside a SUB or FUNCTION,

 but in this case the variables used in the GOSUB are those Global with SHARED into the main and those local of the SUB where GOSUB is.

Guess global has different levels of being "global".
Without SHARED, then the variables are not seen within SUBs.

Thanks for sharing ... no pun intended ;)
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 20, 2019, 09:37:02 pm
If you use DIM SHARED in your main code, it creates global variables that are accessible in all SUBs and FUNCTIONs.

If you use DIM SHARED from within a SUB or FUNCTION, it makes those particular variables global only within that SUB or FUNCTION.  Using it within the SUB or FUNCTION makes cleaner code, as you know at first glance what is being used globally.
Title: Re: GOSUB VS SUB
Post by: Raptor88 on April 20, 2019, 09:44:47 pm
If you use DIM SHARED from within a SUB or FUNCTION, it makes those particular variables global only within that SUB or FUNCTION.  Using it within the SUB or FUNCTION makes cleaner code, as you know at first glance what is being used globally.

Ah, helpful to know info about SHARED within a SUB or FUNCTION.
Thanks,
Raptor88
Title: Re: GOSUB VS SUB
Post by: bplus on April 20, 2019, 10:23:01 pm
If you use DIM SHARED in your main code, it creates global variables that are accessible in all SUBs and FUNCTIONs.

If you use DIM SHARED from within a SUB or FUNCTION, it makes those particular variables global only within that SUB or FUNCTION.  Using it within the SUB or FUNCTION makes cleaner code, as you know at first glance what is being used globally.

Uh? can't use SHARED in a SUB
Title: Re: GOSUB VS SUB
Post by: FellippeHeitor on April 20, 2019, 10:30:32 pm
Can't use DIM SHARED in a SUB/FUNCTION, but you can use SHARED, which allows you to access a "normal" variable from main module:

Code: QB64: [Select]
  1. a = 5
  2. mySub
  3.  
  4. SUB mySub
  5.     SHARED a
  6.     PRINT a
Title: Re: GOSUB VS SUB
Post by: bplus on April 21, 2019, 08:19:21 am
Thanks Fellippe,

For some reason I had SHARED completely locked in with DIM thus IDE red-lined whenever I tried to use DIM SHARED in a sub.

So Raven_Singularity's point of using SHARE in procedures to keep code "cleaner" sounds good, clearer document. I will try that in future.
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 21, 2019, 11:05:01 am
Ah, it's been years since I used SHARED in a SUB/FUNCTION.  I thought it was DIM SHARED even from within SUBs.  Thanks for the clarification, FellippeHeitor.


Edit:
It's clearer because you can see at a glance what variables are being used by the SUB/FUNCTION.  Also when you create new SUBs/FUNCTIONs, you don't need to remember all your globals variables to avoid using them, you just add them on an as-needed basis.
Title: Re: GOSUB VS SUB
Post by: Pete on April 21, 2019, 11:21:35 am
Real "Globalists" use COMMON SHARED. I hate globalists. America First!

Pete
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 21, 2019, 11:51:15 am
I had to go look up COMMON SHARED, never used it before, though I have read the Help page on it in QuickBASIC a few times.  I guess I never needed multiple modules in my apps.
Title: Re: GOSUB VS SUB
Post by: Pete on April 21, 2019, 11:58:18 am
In QBasic days, multimodular programming was the only way you could surpass the 64K limit. Basically, when you reached about 55K you were screwed with an out-of-memory error. To get around that limitation, you created modules and linked them. I discovered you could squeak out 4 to 5 modules, for around 250K of code.

Pete
Title: Re: GOSUB VS SUB
Post by: Raven_Singularity on April 21, 2019, 12:57:49 pm
I only ran into the 64KB limit once in QuickBASIC.  I think it was a few thousand lines of code, along with a bunch of game level data in the source code.
Title: Re: GOSUB VS SUB
Post by: TempodiBasic on April 21, 2019, 02:28:58 pm
@Raptor88

here link of rules:
http://www.qb64.org/wiki/DIM (http://www.qb64.org/wiki/DIM)
http://www.qb64.org/wiki/SHARED (http://www.qb64.org/wiki/SHARED)<--- see example two
http://www.qb64.org/wiki/COMMON (http://www.qb64.org/wiki/COMMON)
http://www.qb64.org/wiki/COMMON_SHARED (http://www.qb64.org/wiki/COMMON_SHARED)
and here a good academic lesson
https://www.youtube.com/watch?v=JqpgyumdBG8 (https://www.youtube.com/watch?v=JqpgyumdBG8)<--- see at 00.41


I'll try to be more clear with my words...
1. if you use only  GOSUB  the variables declared into main are global and accessible from any block of GOSUB you try to access them.


2. if you use only SUB/FUNCTION the variables declared into main are global and accessible from SUB/FUNCTION in 3 ways :
                                                                   1.  DIM SHARED (or COMMON SHARED  if you use more than one module of Qbasic ) in the main
                                                                   2.  DIM (or raw declaration by name with suffix for the type of data) in the main and SHARED with same name in each SUB/FUNCTION where it is accessed
                                                                   3.  DIM (or raw declaration by name with suffix for the type of data) in the main and declaration of the variable into the declaration of the SUB/FUNCTION as parameter.
So yes global are those in the main, the closed block of code SUB/FUNCTION can access to the main in one of the three ways
and the second is this your response...

Quote
Without SHARED, then the variables are not seen within SUBs.

if you use GOSUB into SUB/FUNCTION, in QB64 this case is managed like a main with its GOSUB block, in fact you cannot jump with GOSUB or GOTO into or out a SUB/FUNCTION,
so a GOSUB inner to a SUB/FUNCTION can access to local variable of SUB/FUNCTION and plus to all global variables of the main using one of the three previous methods....
So it is build Qbasic/QB64! 

Thanks to read and thanks for feedback