Page 1 of 1

Building an include .bas file

Posted: Tue May 30, 2023 6:58 pm
by jmcdougall
I have a group of subs that will be common across several programs/processors. There are variables that are currently Dim'd in the program where I created them. If I want to move these subs into an include file, should I move the DIm's for the variables they use into the include file as well, or do the Dim's need to be in each program that does an include?

Re: Building an include .bas file

Posted: Wed May 31, 2023 8:09 pm
by basicchip
If they are all SUB of FUNCTION, any variables passed to the function are in their definition.

Any variables DIM'd inside the function are local to that function.

So there should not be any variables defined outside the SUB or FUNCTION. Though there is no rule against it, it is just good practice not to declare global variables in an #include

Though a SUB or FUNCTION can access global variables that are already declared, though sometimes necessary is probably not a good idea if you want to reuse that code.

Re: Building an include .bas file

Posted: Thu Jun 01, 2023 6:24 pm
by jmcdougall
I have 22 I2C subs for handling the display and its associated LEDs and keypad. Current I have the Dim statements for i2cpresent, i2cresponse, LCD_String, msgstring in the primary program as Global Variables. These are used in almost all subs so I would rather keep them in the main program as opposed to Dims in every sub. Based on your response, I should keep these as Global declarations, and the #include displaysubs would be subs only, no other code.

Re: Building an include .bas file

Posted: Thu Jun 01, 2023 6:34 pm
by basicchip
You can not DIM the same variable multiple times. So your code would take the form of

Code: Select all

DIM global1
DIM global2     '...

#include one.bas
#include two.bas    '...

main: