Building an include .bas file

Questions about the BASICtools and MakeItC
Post Reply
jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Building an include .bas file

Post 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?



basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Building an include .bas file

Post 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.

jmcdougall
Posts: 35
Joined: Tue Mar 22, 2022 11:17 pm

Re: Building an include .bas file

Post 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.

basicchip
Posts: 1090
Joined: Fri Oct 19, 2012 2:39 am
Location: Weeki Watchee, FL
Contact:

Re: Building an include .bas file

Post 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:

Post Reply