Creating Programs

Unlike most DOS versions of BASIC, PB/CC requires that all executable code be contained inside a Sub or Function.  Only scanned statements (such as metastatements, constant declarations, and procedure declarations) can be placed outside of Subs and Functions.  Therefore, every PB/CC program must have either a PBMAIN or a WINMAIN function so Windows can identify where to start executing your program.  PBMAIN and WINMAIN are special functions reserved for exactly this purpose.

The following PB/DOS program:

PRINT "At the tone the time will be "; TIME$

BEEP

simply needs to be placed inside a Function called PBMAIN in order to execute:

FUNCTION PBMAIN

  PRINT "At the tone the time will be "; TIME$

  BEEP

END FUNCTION

When your program is loaded, the function PBMAIN (or WINMAIN) is executed automatically.  This is where the main code of your application should be placed.  The program stops when there are no more statements in PBMAIN, or an EXIT FUNCTION is encountered within PBMAIN.

If you want to return an error level back to the calling program (or DOS/command prompt) you can assign it as the result to the PBMAIN function:

FUNCTION = 5

or:

PBMAIN = 5

Any value larger than 255 is simply ignored, causing 0 (the default) to be returned.

By default, all variables are LOCAL to Subs and Functions, which means other Subs or Functions cannot see them.  If you need to share a variable or array throughout your program, you should use the GLOBAL statement to change the scope.  See Variables and Variable Scope for more details.

In DOS BASIC, all variables without a type-specifier (such as % for Integer, ! for Single-precision, etc) default to Single-precision.  In PB/CC you must either use a type-specifier with each variable or explicitly declare its type using the DIM, LOCAL, GLOBAL, or THREADED statements:

a% = 1

or:

DIM a AS INTEGER

a = 5

Optionally, you can use a DEFx statement to declare the default variable type.  Using DEFSNG would implicitly classify all variables as Single-precision, the same as the default in DOS programs.  Explicit declarations take precedence over the implied action of DEFx.  For a complete list of differences between PB/CC and DOS BASIC, please see Appendix B.

We recommend that programmers get into the habit of using #DIM ALL or OPTION EXPLICIT in their programs to ensure all variables are declared correctly.  While it may take a little extra time, this approach can often prevent subtle bugs from using a variable with the wrong name.

For example, MyVeryLongInteger% could be easily confused with MyVeryLongIntger%, but if an explicit declaration of the variable were made, this type of simple error would not have occurred.  This is because the compiler would have flagged the variable with the wrong spelling, rather than accepting it as a separate variable.

While retrofitting #DIM ALL or OPTION EXPLICIT to existing code might be a significant undertaking if the code is large, the effort is often worthwhile.

 

See Also

Writing Programs in PBCC

What is a Console?

Hello, World! Example

Line numbers and Labels

Long lines

Statement separation

Structured Programming