Subprograms
Volume Number: 2
Issue Number: 2
Column Tag: Special Projects
Subprograms in Basic Explained
By Mike Steiner, Sierra Vista, AZ, MacTutor Contributing Editor
Versatile Input Routine
So, you are writing a program in Microsoft BASIC and the user of the program
has to supply information for the program to process. The tradtional, and - before
Microsoft BASIC for the Macintosh - sometimes the only available, way for a BASIC
program to obtain data from the keyboard is via the INPUT statement. This yeoman
method has worked for many years and has proven its worth. It also has shown all its
faults. In some varieties of BASIC, certain characters (notably the comma and colon)
cannot be read by INPUT. Unless you incorporate error checking routines (which can
require extensive programming), the only way the user can correct errors is for him
to notice the error before he presses Return so that he can backspace and retype his
data. Error checking routines range from a simple
INPUT "Do you want to make any changes? (Y/N) "; YN$
followed by more questions and INPUT statements, to complex screen structures with
vertical and horizontal tabbing combined with the INPUT statement.
If the program uses formatted displays, then more programming effort may be
required to reformat the screen for the display after each INPUT sequence.
Microsoft BASIC for the Macintosh (Version 2.0 or later) has some built-in
features that can help you alleviate some, if not all, of these problems. Formatted
screen displays can be kept intact by overlaying windows for the input routines. INPUT
can be replaced by EDIT FIELD and EDIT$ to provide a powerful error-checking routine
that allows the operator to check all of his data before signifying that everything is
correct and proceeding to the next task.
Listing 1 contains a routine just for this purpose. The routine is in the form of a
subprogram so that it can be inserted anywhere in your program. Sub- programs are,
to my knowledge, unique to the Macintosh version of BASIC. They are independent
modules that can be anywhere within a program listing and are completely ignored by
BASIC unless they are CALLed by the program. All variables used by a subprogram
remain with the subprogram and do not affect the variables in the main program unless
explicitly allowed to do so by the program. If A = 10 in the main program and a
subprogam sets A equal to 136.43, when the subprogram exited, A would still equal 10
in the main program. Pascal programmers will recognize subprograms as being very
similar to Pascal's procedures.
The listing has two parts. The first is an example of a main program for the
subprogram to work within. The second is the the subprogram itself.
The main program starts by setting all variables as integers. It then DIMensions
the arrays label$, info$, and corner. Label$ contains the names of the edit fields;
info$ holds the actual information inputted, and corner defines the four corners of the