Introduction to True BASIC

The BASIC programming language was developed in 1965 by John G.
Kemeny and Thomas E. Kurtz as a language for introductory courses in
computer science. In 1988 they extended the language to make it
modular and portable.

1. Introduction
True BASIC, C, Fortran, and Pascal are examples of procedural
languages. Procedural languages change the state or memory of the machine by a sequence of statements. True BASIC is similar to F (a subset of Fortran 90) and has excellent graphics capabilities which are
hardware independent. True BASIC programs can run without change on computers running the Macintosh, Unix, and Windows operating systems. We will consider version 3.0 (2.7 on the Macintosh) of True BASIC. Version 5 includes the ability to build objects such as buttons, scroll bars, menus, and dialog boxes. However, because we wish to emphasize the similarity between True BASIC and other
procedural languages such as C, F, and Java, we do not consider these features.

There is no perfect programming language (or operating system) and users should be flexible and choose the appropriate language to accomplish their goals. Former students who were well grounded in True BASIC have had no trouble learning C, F, and Java quickly.

The first statement is an optional PROGRAM header. The inclusion of a program header is good programming style.

Comment statements begin with ! and can be included anywhere in the program.

PROGRAM, LET, PRINT, and END are keywords (words that are part of the language and cannot be redefined) and are given in upper case. The case is insignificant (unlike C, F, and Java). The DO FORMAT command converts keywords to upper case.

The LET statement causes the expression to the right of the = sign to be evaluated and then causes the result to be assigned to the left of the = sign. (The LET statement reminds us that the meaning of the = symbol is not the same as equals.) It is not necessary to type LET, because the DO FORMAT command automatically inserts LET where appropriate. The LET statement can be omitted if the OPTION NOLET statement is included.

True BASIC does not distinguish between integer numerical variables and floating point numerical variables and recognizes only two types of data: numbers and strings (characters). The first character of a variable must be a letter and the last must not be an underscore.

Note the difference between the INPUT and INPUT prompt statements and the simple modification of the PRINT statement. What happens if you replace the semicolon after the expression in the PRINT statement by a comma? Modify the program so that it adds, subtracts, and divides two numbers.

2. Loop structures
True BASIC uses a FOR or a DO construct to execute the same statements more than once. An example of a FOR loop follows:

The use of the FOR loop structure allows a set of statements to be executed a predetermined number of times. The index or control variable (n in Program series) monitors the number of times the loop has been executed. The FOR statement specifies the first and last value of the index and the amount that the index is incremented each time the NEXT statement is reached. Unless otherwise specified, the index is increased by unity until the index is greater than its last value in which case the program goes to the statement after the NEXT statement. In Program series, the index n assumes the values 1 through 100. True BASIC treats n as an integer variable.

The block of statements inside the loop is indented for clarity. Use the DO FORMAT command to indent loops automatically.

The order of evaluation follows the mathematical conventions shared by all computer languages. Exponentiations are performed first, followed by multiplications and divisions from left to right. Parentheses should be used whenever the result might be ambiguous to the reader. The parentheses in the statement, LET sum = sum + 1/(n*n), are included for clarity.

All unassigned variables are automatically initialized to zero. Because C, F, and Java do not, it is recommended that variables such as sum in Program series be initialized explicitly.

In many cases the number of repetitions is not known in advance. An example of a DO loop follows:

Although True BASIC explicitly recognizes only two kinds of variables, numeric and string, it implicitly distinguishes between floating point (real) and integer numeric variables. For example, the variable x in Program decision2 is treated as an integer variable and hence stored with infinite precision; the variable f is treated as a real variable and stored to 14 to 16 decimal places depending on the computer. Arithmetic with numbers represented by integers is exact, but arithmetic operations which involve real numbers is not. For this reason, decision statements should involve comparisons of integer variables rather than floating point variables.

C and Fortran 90 support the WHILE statement, but F does not. A program equivalent to Program decision2 is given in the following:

It is convenient to divide a program into smaller units consisting of a main program and subprograms consisting of subroutines and functions. Subprograms are called from the main program or other subprograms. As an example, the following program adds and multiplies two numbers which are inputed from the keyboard.

Program tasks is an example of a modular
program, a program which is divided into separate tasks each of which can be written and tested separately. A complete program contains a main program consisting of a series of calls to subprograms. Subroutines are invoked by the
CALL statement. Subroutines and
functions are called from the main program or other subprograms.

A subroutine is defined by a SUB statement; the end of a subroutine is denoted by END SUB. We use only external subroutines and functions. External program units are defined in any order after the END statement of the main program.

A subroutine is a separate program unit with its own local variables, that is, the variables in the main program and in each external subroutine and function are available only to the program subunit. A variable name represents a memory location in the computer. If the same variable name is used in two program units, the name represents two different memory locations.

The most common method for subroutines to pass
information to the main program and to other subroutines is via
arguments in the subroutine calls. In Program tasks the variables x,
y, sum, and product are passed in this way. (It is a little misleading to say that variable names are passed to a subroutine. More precisely, the variable names are not passed, but rather the memory location in the computer is passed. The variable name is simply a label that identifies the memory location.)

In Program no_pass the variable name x is used in the main program and in SUB add_one, but is not passed. Convince yourself that the result printed for x in the main program is 10. What is the value of x in SUB add_one?

PROGRAM no_pass
LET x = 10 ! local variable name x defined in main program
CALL add_one
PRINT x
END
SUB add_one
! variable name x not passed
LET x = x + 1 ! local variable name defined in subroutine
END SUB

What are the values of x and y if SUB add_one in Program pass is written in the following form?

If you are uncertain how a particular function works, write a little program to test it. For example, what is the value of mod(10,3)? What about mod(-10,3)? What is the difference between the MOD and the REMAINDER functions?

Program single_column uses a
string or character variable for the name of the file.
The writing of files with multiple columns is more complicated and is illustrated in the following.

file$ = "config.dat"
OPEN #1: name file$,access output,create new
PRINT #1: N
PRINT #1: L
! comma added between inputs on the same line so that file
! can be read by True BASIC
FOR i = 1 to N
PRINT #1, using "----.######, ----.######": x(i),y(i)
NEXT i
CLOSE #1
! next illustrate how to read file.
OPEN #1: name file$, access input
INPUT #1: N
INPUT #1: L
FOR i = 1 to N
INPUT #1: x(i),y(i)
NEXT i

It is necessary to separate columns by a comma, an annoying feature of True BASIC. There are many variations on the open statement, but the above example is typical.

A graphics screen is covered by a grid of pixels. The number of
pixels is hardware dependent. In True BASIC the number of pixels is
irrelevant because the mapping of the absolute values of the
coordinates to the device coordinates or pixels is done by True
BASIC. The first step is to specify the
range of coordinates which are to be plotted. The statement

SET WINDOW xmin, xmax, ymin, ymax

determines the minimum and maximum x (horizontal) and y
(vertical) coordinates. The statement

PLOT POINTS: x,y;

draws a point at (x,y) in the current window coordinates. The
statement

PLOT LINES: x1,y1; x2,y2;

draws a line between (x1,y1) and (x2,y2). Program plot_f uses these statements to draw a set of axes and
plot the function T(t) = Ts - (Ts - T0) e-rt.

The program uses a separate subroutine to set up the screen and
another to plot the function.

PLOT x,y; is an abbreviation of PLOT POINT x,y.

Program simple_map uses the SET WINDOW, PLOT, BOX LINES and ASK MAX COLOR statements to help visualize the trajectory of a two-dimensional dynamical system. A summary of the some of the important graphics statements is given in Table 3.

PROGRAM cool
! numerical solution of Newton's law of cooling
LIBRARY "common"
DECLARE PUBLIC r,t,dt,tmax,nshow,T_coffee
CALL output
LET counter = 0
DO
IF (t >= tmax) then
EXIT DO
END IF
CALL Euler
LET counter = counter + 1 ! number of iterations
IF (mod(counter,nshow) = 0) then
CALL output
END IF
LOOP
DO
LOOP until key input
END

The module "common" is in a separate file with the same name. PUBLIC statements determine which variables may be accessed from outside the module. SHARE statements determine the variables that are available to all subprograms within the module, but not outside the module.