BASIC LESSON 9

Lesson Topics
Arrays (Higher Dimensional)Subprograms (Editing)
SubprogramsSHARED
DECLARECOMMON SHARED
CALLDIM SHARED
Arrays in Subprograms
View DemosDownload Demos
# 1 # 2 # 3 # 4 # 1 # 2 # 3 # 4
Main Basic Page

Higher Dimensional Arrays

A higher dimensional array is a list depending on more than one index. The simplest example is an m x n matrix,
Such a matrix, depending on two indices, is a two-dimensional array. The rows of the matrix run from left to right and the columns from top to bottom. In Basic we might index the rows by I and the columns by J, so that the matrix entry occurring in the I-th row and J-th column is referred to as A(I,J). It is possible also to work with arrays of three dimensions or more, but in mathematical programming such larger arrays are uncommon.

To introduce a higher dimensional array you insert a dimension statement. A possible dimension statement for the above m x n matrix is

DIM A(1 TO M, 1 TO N).

Other examples of dimension statements are

DIM A(1 TO 5, 0 TO 6)
DIM B(-3 TO 6, 4 TO 7, 5 TO 100).

As with one-dimensional arrays, the type of an array can be indicated by adding a symbol after its name. The expressions

DIM P#(1 TO 3, 1 TO 4)
DIM info$(0 TO K, 1 TO L, 2 TO M)

refer to a two-dimensional array P# of double precision numbers, and a three-dimensional array info$ of strings.

Working with higher dimensional arrays sometimes requires the use of nested loops. Here is how you might prompt the user for the entries in an M x N matrix A:

DIM A(1 TO M, 1 TO N)
FOR I = 1 TO M
FOR J = 1 TO N
PRINT "What is the entry in row"; I; "and column"; J;
INPUT A(I,J)
NEXT J
NEXT I

The outer loop, indexed by I, runs through the rows, while the inner loop, indexed by J, runs through the columns. The double loop asks the user to enter the entries in the first row, from the leftmost column to the rightmost column, then those in the second row from left to right, then the third row, etc., all the way to the last row.

Subprograms

Subprograms are useful for dividing up long programs into shorter and more manageable pieces. Typically a subprogram will handle just one assignment in a main program, while the job of the main program is to shuffle the action back and forth in an organized way between all the subprograms. A well-written subprogram, specializing in a particular task, can be copied into other main programs requiring that same task; in this way much programming time can be saved.

We begin with a simple example. Here is a procedure that adds two numbers X and Y, and prints their sum:

Z = X + Y
PRINT "The sum is "; Z

We can write this procedure as a subprogram, named "ADD", as follows:

SUB ADD (X, Y)
Z = X + Y
PRINT "The sum is "; Z
END SUB

Now here is a main program, using ADD as a subprogram:

DECLARE SUB ADD (X!, Y!)
INPUT "What is the first number you want to add"; A
INPUT "What is the second number you want to add"; B
CALL ADD(A,B)
END

When the main program reaches the CALL statement, the subprogram substitutes the values of A and B for X and Y, and then runs itself. When the subprogram is finished, the main program takes over and proceeds to the next statement. The DECLARE statement in the main program warns Basic that ADD is a subprogram which might be called. Every subprogram has its own DECLARE statement - and all DECLARE statements appear at the beginning of the main program, before any of the statements that are to be executed. The exclamation points in the DECLARE statement indicate that X and Y are single precision variables (by default, since the subprogram does not specify otherwise).

One problem with the above scheme is that the main program never finds out the sum of the two numbers (although this sum is printed for the user to read). If the main program will need the sum for some purpose, then the subprogram must communicate this sum back to the main program. One way of doing this is to insert a SHARED statement into the subprogram, as follows:

SUB ADD (X, Y)
SHARED Z
Z = X + Y
PRINT "The sum is"; Z
END SUB

Then the subprogram "shares" the value of Z with the main program - that is, before it terminates it informs the main program of the value of Z.

A second way of informing the main program of the sum is to leave the subprogram alone, but insert a COMMON SHARED statement into the main program. (SHARED statements go in subprograms, and COMMON SHARED statements in main programs.) Here is one way of doing it:

DECLARE SUB ADD (X!, Y!)
COMMON SHARED Z
INPUT "What is the first number you want to add"; A
INPUT "What is the second number you want to add"; B
CALL ADD (A, B)
END

The COMMOM SHARED statement ensures not only that the main program and the subprogram "ADD" share the value of Z, but also all other subprograms share this value.

Finally, a third way to inform the main program of the sum is to forget about SHARED statements, but let the subprogram depend on three variables, as follows:

SUB ADD (X, Y, Z)
Z = X + Y
PRINT "The sum is "; Z
END SUB

Then we change the call statement to

CALL ADD(A, B, sum) .

The main program will now look like

DECLARE SUB ADD (X!, Y!, Z!)
INPUT "What is the first number you want to add"; A
INPUT "What is the second number you want to add"; B
CALL ADD(A, B, sum)
END

When the subprogram runs, it substitutes A for X, B for Y, and sum for Z. (Since sum has not yet been assigned a value, Basic assigns it the default value 0.) After the subprogram runs, it reassigns the value of X back to A, the value of Y back to B, and the value of Z back to sum - the main program accepts these values as those of A, B, and sum. This third method is probably the preferred method if the subprogram is later to be copied and used in other programs, as another program might have different names for variables and the SHARED statements can then lead to conflicts.

In general, suppose a subprogram has a bunch of parameters, such as

SUB FISH (V, W, X, Y, Z) .

Then a statement calling this subprogram might look something like

CALL FISH (A, B, C, D, E) .

(Note that the number of variables in the CALL statement matches the number in the subprogram.) When the subprogram FISH executes, it substitutes A for V, B for W, C for X, D for Y, and E for Z. When the subprogram is done, some or perhaps all of the values V, W, X, Y, Z will have changed. The subprogram then substitutes V back for A, W for B, X for C, Y for D, and Z for E, and the main program accepts these new values for A, B, C, D, E.

Variables in a subprogram may be strings or arrays as well as numbers. The types of these variables will be reflected in the DECLARE statement for the subprogram, and the types of the variables in the CALL statement must match the types in the DECLARE statement. A violation of these rules will lead to "type mismatch" error messages in Basic. You must be careful that types are correctly specified in DECLARE statements; if you do not specify a variable type, Basic by default makes it single precision.

Following is a simple program on the left with its subprogram on the right:

DECLARE SUB DEMO (kid$, age%) SUB DEMO (kid$, age%)
A$ = "Joey" kid$ = "Big " + kid$
X% = 5 age% = age% + 1
CALL DEMO(A$, X%) END SUB
PRINT "Next year "; A$; " will be"; X%
END

Upon being called, the subprogram substitutes A$ = "Joey" for kid$, and X% = 5 for age%. It adds "Big " to kid$ to get kid$ = "Big Joey", and adds 1 to age% to get age% = 6. Then it substitutes back A$ = kid$ = "Big Joey" and X% = age% = 6. Finally, the main program prints "Next year Big Joey will be 6".

Arrays in Subprograms

If a subprogram uses an array such as A(1 to N) for example, then this is signified by including A() in parentheses after the subprogram name. The subprogram

SUB MOUSE (K%, A(), B#)

has as parameters an integer K%, a single precision array A, and a double precision variable B#. Note that you do not include the dimensions of the array - just use empty parentheses to signify the array. A CALL statement for the above program might be something like

CALL MOUSE(N%, List(), R#) .

When the subprogram MOUSE runs, it substitutes N% for K%, the array List for array A, and R# for B#. After the subprogram completes its run, it makes the substitutions in reverse.

Remember that a subprogram with an array cannot run until it knows the dimension of the array. Thus in the above example the dimension of List must be declared before it is placed in the CALL statement, as otherwise the subprogram will stop and complain that the array has not been defined. If the dimension of List has indeed been declared, the subprogram will read this dimension along with the values in the array.

This subprogram averages the numbers in an array A, indexed from 1 to K%:

SUB AVERAGE (K%, A(), avg)
S = 0
FOR I = 1 TO K%
S = S + A(I)
NEXT I
avg = S/K%
END SUB

If the main program were working with an array W, indexed say from 1 to N%, it could compute the average (or mean) of the numbers in the array with the statement

CALL AVERAGE (N%, W(), mean) .

The subprogram substitutes N% for K%, W for A, and mean for avg. (The variable "mean" probably will not have been specified and thus will have the default value 0.) After the subprogram runs, it substitutes in reverse and the new value of mean becomes that of avg.

A common mistake lies in defining the dimension of an array twice - once in the main program and again in the subprogram. In this event the subprogram gives an error message saying the array has already been defined.

Editing Subprograms

General subprograms, like function subprograms, are edited in a separate window in Basic. When you enter the name of a subprogram, Basic opens its special subprogram window. In this window already appears the name of the subprogram and the final END SUB statement, and in between you are to type the body of the subprogram. You get back to the main program by clicking "View - Subs" and the name of your main program.

When you print a Basic program with subprograms, the main program prints first and then all the subprograms in alphabetical order.

When you save a Basic program with a subprogram and a CALL statement referring to it, Basic automatically inserts a corresponding DECLARE statement at the beginning of the program. (You can preempt Basic by writing the DECLARE statement yourself, or you can let Basic insert the DECLARE statement and later modify it.)

SHARED

The main program and subprograms do not automatically share information not listed in CALL statements. If you want the main program and a subprogram to share knowledge of the value of certain variables, you may insert a SHARED statement in the subprogram. For instance, the statement

SHARED X

in a subprogram guarantees that whenever either the main program or the subprogram assigns or modifies a value for X, both the main program and subprogram will know its new value. You may mention more than one variable in a SHARED statement, as in

SHARED student$, age, address$ .

A SHARED statement may appear only in a subprogram; if you try to type a SHARED statement in the main program you get an error message.

COMMON SHARED

A COMMON SHARED statement may appear only in the main program - it instructs Basic that the variables listed are to be shared between the main program and all subprograms. The command

COMMON SHARED X, Z

ensures that the values of X and Z will be known by the main program and all subprograms. If the main program or any subprogram assigns or changes X or Z, the main program and all subprograms will be sure to know about it.

To declare that a variable A is a shared array, insert the statement

COMMON SHARED A() .

(Do not include the dimension of the array - just type empty parentheses.)

DIM SHARED

A DIM SHARED statement may appear only in the main program. The statement

DIM SHARED A(1 TO 5, 1 to 3)

will guarantee that the main program and all subprograms know that A is a 5 x 3 array, and that they share the values of the entries in this array.