Author Topic: sub placement in program  (Read 3945 times)

0 Members and 1 Guest are viewing this topic.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
sub placement in program
« on: September 30, 2020, 01:15:00 pm »
Hello

why do subs have to go at the bottom of any other commands, like gosubs and such.

i hope that explains my question


Badger

FellippeHeitor

  • Guest
Re: sub placement in program
« Reply #1 on: September 30, 2020, 01:17:18 pm »
As soon as you create the first sub/function you end the main module. It has been discussed to change that behaviour, since the original basic compiler allowed for subs to be anywhere, but we ended up keeping up with the QuickBASIC IDE, which did move every module to the end.

Offline badger

  • Forum Regular
  • Posts: 148
    • View Profile
Re: sub placement in program
« Reply #2 on: September 30, 2020, 01:27:43 pm »
Hello

Ok i was just wondering. The dos c compiler all functions had to come before the main i think i remember that. but just wondering

Badger

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: sub placement in program
« Reply #3 on: September 30, 2020, 08:13:06 pm »
That was an issue I discovered recently with respect to C++. I was used to the main part being first and the subroutines following after. It took a while for me to break out of my Qbasic think to grasp the opposite of C++. It didn't take as long as going from Applesoft to Qbasic. Procedural programming was a new paradigm that I had a time wrapping my head around. I've come to prefer it now.

I can't speak for others, but I typically use the main module to set up the program's base environment; type & variable declarations, data fields, and such, after which I pass control to a "main loop" SUB, from which all others are called according to inputs received. I almost always do larger projects in this manner, opting for more cyclic than linear arrangements. As such, I don't usually run a program from the main module, but rather it uses a hierarchy of SUBs and FUNCTIONs for primary flow. When things are arranged in a highly modular form like that, I find bug hunting to be much easier. It also doesn't matter what order your code is arranged in for execution, you can put it in any arrangement that facilitates your needs, function-wise, alphabetic, or whatever. You can also export useful routines to other programs much easier.

I rarely use gosubs anymore, as a SUB generally does the same thing, the only exception being when I'm passing local variables within a SUB or FUNCTION. Even then, I could probably design it better and SUB those out, and if I'm tempted to use a GOTO, I hit myself with a rolled up newspaper. I should probably exorcise gosub in the same manner...

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: sub placement in program
« Reply #4 on: October 01, 2020, 10:50:34 am »
about
Quote
since the original basic compiler allowed for subs to be anywhere, but we ended up keeping up with the QuickBASIC IDE, which did move every module to the end

I have no experience of BASICA or other previous Basic language dialects, I started with Qbasic /QB4.5.
In this widespread dialect of BASIC the subprocedures (SUBs and FUNCTIONs) are put after the END of the main program, BUT you need to DECLARE ALL SUBs and FUNCTIONs at the start of the main module, at top of all the declarations and definitions and initializations.... so I have thought about this behaviour like in C/C++ where you first create the template and then you create the instances of that template. QB64 doesn't use DECLARATION of SUBs and FUNCTIONs of the program so in this it is very different from QB4.5...
I find difficult , for my forma mentis, to think to create a SUB in the middle of the main module, it is like to go back (for my point of view) to the old spaghetti code style in which you make SUB using GOTO Labels and GOSUB Labels/RETURN.

My think about building a program is nearer to that of OldMoses that creates a hierarchy of SUB and FUNCTION, in which I see each SUB or FUNCTION like a lego piece closed to the outer and flexibile to be used in different ways
Thanks to read
Programming isn't difficult, only it's  consuming time and coffee

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: sub placement in program
« Reply #5 on: October 01, 2020, 11:23:41 am »
C compilers will read a file from top to bottom, once. Before you can call a function, the compiler must have seen a definition or a declaration (i.e. a prototype) of it previously in the file.

QB64 can be considered a two-pass compiler because you can call a function that hasn't been defined yet (even within a function, you can call a function that is defined later), but as mentioned it will insist you give the main program first.