$INCLUDE

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search

$INCLUDE is a metacommand that is used to insert a source code file into your program which is then executed at the point of the insertion.


Syntax

{REM | ' } $INCLUDE: 'sourceFile'


Description

  • QBasic metacommands must be commented with REM or an apostrophe.
  • The sourceFile name must be enclosed in apostrophes and can include a path.
  • $INCLUDE is often used to add functions and subs from an external text QBasic code library.
  • The $INCLUDE metacommand should be the only statement on a line.

How to $INCLUDE a BAS or Text file with a QB64 Program

  • Assemble the code to be reused into a file.
  • Declarations (e.g. TYPE, DIM, CONST, SHARED), DATA, SUBs, and FUNCTIONs may all be in the same file which is included at the beginning of a program.
  • GOSUB and ERROR handlers can be in the same file with a GOTO to skip execution over them, or they may be in a separate file included at the end of a program.
  • By convention, the extension .BI is used for files included at the beginning of a program and .BM for files included at the end of a program. These extensions are not required, any extension can be used as long as the file contains code in plain text (binary files are not accepted).
  • Compile the program.
  • Note: Once the program is compiled, the included text files are no longer needed with the program EXE.


Availability

  • Versions before QB64-PE v4.4.0 did not allow interleaving main level code with SUB and FUNCTION definitions, so these definitions had to be at the end of the program (usually in a separate .BM file).

Examples

Example
Showcasing a simple program which includes another source file.
The Main program file.
'$INCLUDE: 'UTILITIES.BI'

'Use a SUB and constant defined in the included file
ShowText ProgramName

'Use a GOSUB handler defined in the included file
GOSUB ShowVersion
The "UTILITIES.BI" file $INCLUDEd by the Main program.
'The included file can contain declarations
CONST ProgramName = "My Program"
CONST Version = "1.3"

'SUB or FUNCTION definitions are allowed
SUB ShowText (text$)
    PRINT text$
END SUB

'GOSUB handlers need to be skipped with a GOTO
GOTO utilities_skip_gosubs
ShowVersion:
PRINT Version
RETURN

utilities_skip_gosubs:

More examples


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link