News:

Let's find out together what makes a PIC Tick!

Main Menu

Where to declare variables

Started by RGV250, Mar 21, 2021, 02:25 AM

Previous topic - Next topic

RGV250

Hi,
I have used this method a few times and the programs all appear to work as expected, in my opinion it makes the code nice and tidy and easier to understand but I wonder if it is OK for the compiler.
In the latest project which is the easiest to explain, below is the structure. variables are declared in FT800_Core, they are also declared in FT800_Commands and then in the Sim codes which are called from the various INC files.
As said, it appears to work fine and in my opinion it makes the code a lot easier but am I just lucky or is it OK to do it this way.

I suppose I should also ask about procedures, they are created mainly in FT800_Core & FT800Commands rather than at the end of the program as recommended. It just makes the program easier to follow in my opinion.

Bob

        Include "amicus18.inc"
        Include "FT800_Core_2_1.Inc"       ' Set SPI pins, Display read/write routines & Initialise screen
        Include "FT800_1_2.INC"            ' FT800 register, memory and command values
        Include "Ft800Commands_2_5.Inc"    ' Commands and widgets (Includes variables used for commands)     
        Include "FT800_Screen_Files_1.Inc"   ' Individual screens included here     
                'Calls        "ScreenCalibrate_2_1.Inc"
                '             "MenuScreen.Inc"
                '             "ButtonsScreen.Inc"               
                '             "NumbersScreen.Inc"   -  Calls Sim Code
                '             "TextScreen.Inc"               
                '             "ClockScreen.Inc"     -  Calls Sim Code             
                '             "GaugeScreen.Inc"     -  Calls Sim Code
                '             "SpareScreen.Inc"
                '             "Touch_X_Y_Screen.Inc"     
                '             "ProgressScreen.Inc"  -  Calls Sim Code                                 
                '             "ToggleScreen.Inc"    -  Calls Sim Code                                     
                '             "KeysScreen.Inc"   
                '             "SliderScreen.Inc"
                '             "LinesScreen.Inc"     
                '             "PointsScreen.Inc"   
                '             "GradientScreen.Inc"   

John Drew

G'day  Bob,
My understanding is that for global variables you can declare them pretty well anywhere, except variables declared inside a procedure are local.
Procedures can be placed anywhere in a program.
This is all because the compiler scans all files in developing the list of variables. Similarly Les explained that the program files are scanned for procedures, they are then moved to the end of the program ready for the compiler. This is why you don't have to provide a jump over procedures.

I do what you do, although as a matter of habit I put procedures at the top of a program or include file out of habit. It is required in Pascal - my favourite language.
Cheers
John

okmn

#2
just to remember
A Typical Procedural BASIC Program Layout
A Typical flat BASIC Program Layout
from proton book