SUBS or GOSUB; for library projects, which do you guys prefer?
#4
(08-27-2022, 07:13 PM)bplus Wrote: The only way you could possibly use a GoSub in a library is to have it inside a Sub or Function.

You can put a GOSUB library anywhere inside your main code.

Code: (Select All)
GOTO skipLibrary2022.08.27
 
proveExample:
PRINT "Here's an example of a GOSUB library!"
RETURN

skipLibrary2022.08.27:

Feel free to save that as a *.BM library file and $INCLUDE it in any of your programs, anywhere in the main module.   Just call it with GOSUB proveExample.



That said, I'd *never* go such a route.  For starters, the routine is *only* callable from the main module -- it won't work with any of your other SUB or FUNCTION modules.  Any variables in it are all in scope, so if you use a FOR i ...NEXT, you better make certain you're not corrupting the value of i elsewhere with it.  There's no easy way to free variables used in the gosub, and all-in-all, it's just one of those tools of the caveman programmer like LET, in my opinion.  I can't think of more than a handful of times in my long years of programming where I thought "GOSUB might be better than SUB for this".  When I did, it was almost exclusively just for use *inside a SUB* itself.  Wink
Reply


Messages In This Thread
RE: SUBS or GOSUB; for library projects, which do you guys prefer? - by SMcNeill - 08-27-2022, 07:48 PM



Users browsing this thread: 3 Guest(s)