[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
QbasicFAQ_Gosub
What's the difference between a SUB module and a GOSUB?
Back to QBasic FAQ Main Page.
QBasic allows the programmer to create SUB modules that are accessed separately from the main module, where GOSUBS are program directions that are invoked at any level.
Similar to a GOTO, GOSUB routes the program instructions to a new line to execute that code. The last line will need to be RETURN, so that program execution returns to the line of below where it left out. QB allows for a RETURN <Line Identifier>, but that also allows for poor programming practice. You should just RETURN where you left from to avoid unexpected results.
SUBs are separate modules that are created with the DECLARE SUB statement. Module level SUBs are great for redundant and recursive operations, and they accept parameters. The parameters are passed at the time of CALL, and usually are what the SUB uses to complete it's work.
Declared SUB routines are unaware of variables in other modules unless they are passed as parameters, or defined as SHARED. (Hint: You can use COMMON SHARED, but it uses more resources than if you DIMension them shared.)
A secondary benefit to creating SUB modules is that the amount of memory used for program loading is not as affected by the module size. Main Module level is restricted to 64K, but module code can be included making it much larger.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
QbasicFAQ_Gosub
What's the difference between a SUB module and a GOSUB?
Back to QBasic FAQ Main Page.
QBasic allows the programmer to create SUB modules that are accessed separately from the main module, where GOSUBS are program directions that are invoked at any level.
Similar to a GOTO, GOSUB routes the program instructions to a new line to execute that code. The last line will need to be RETURN, so that program execution returns to the line of below where it left out. QB allows for a RETURN <Line Identifier>, but that also allows for poor programming practice. You should just RETURN where you left from to avoid unexpected results.
SUBs are separate modules that are created with the DECLARE SUB statement. Module level SUBs are great for redundant and recursive operations, and they accept parameters. The parameters are passed at the time of CALL, and usually are what the SUB uses to complete it's work.
Declared SUB routines are unaware of variables in other modules unless they are passed as parameters, or defined as SHARED. (Hint: You can use COMMON SHARED, but it uses more resources than if you DIMension them shared.)
A secondary benefit to creating SUB modules is that the amount of memory used for program loading is not as affected by the module size. Main Module level is restricted to 64K, but module code can be included making it much larger.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
