Author Topic: GOSUB VS SUB  (Read 12036 times)

0 Members and 1 Guest are viewing this topic.

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #45 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 ;)

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #46 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.

Offline Raptor88

  • Newbie
  • Posts: 22
    • View Profile
Re: GOSUB VS SUB
« Reply #47 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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: GOSUB VS SUB
« Reply #48 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

FellippeHeitor

  • Guest
Re: GOSUB VS SUB
« Reply #49 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

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: GOSUB VS SUB
« Reply #50 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.
« Last Edit: April 21, 2019, 08:21:16 am by bplus »

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #51 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.
« Last Edit: April 21, 2019, 11:09:35 am by Raven_Singularity »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: GOSUB VS SUB
« Reply #52 on: April 21, 2019, 11:21:35 am »
Real "Globalists" use COMMON SHARED. I hate globalists. America First!

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

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #53 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.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: GOSUB VS SUB
« Reply #54 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
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Raven_Singularity

  • Forum Regular
  • Posts: 158
    • View Profile
Re: GOSUB VS SUB
« Reply #55 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.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: GOSUB VS SUB
« Reply #56 on: April 21, 2019, 02:28:58 pm »
@Raptor88

here link of rules:
http://www.qb64.org/wiki/DIM
http://www.qb64.org/wiki/SHARED<--- see example two
http://www.qb64.org/wiki/COMMON
http://www.qb64.org/wiki/COMMON_SHARED
and here a good academic lesson
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
« Last Edit: April 21, 2019, 03:04:55 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee