Subscripts

Instead of being a single string, x$ can be a whole list of strings, like this:

"love"

"hate"

"kiss"

x$= "kill"

"peace"

"war"

"why"

Here’s how to make x$ be that list of strings.…

Begin your program as usual, by saying:

CLS

Then say:

DIM x$(7)

That line says x$ will be a list of 7 strings. DIM means dimension; the line says the dimension of x$ is 7.

Next, tell the computer what strings are in x$. Type these lines:

x$(1) = "love"

x$(2) = "hate"

x$(3) = "kiss"

x$(4) = "kill"

x$(5) = "peace"

x$(6) = "war"

x$(7) = "why"

That says x$’s first string is "love", x$’s second string is "hate", etc.

If you want the computer to print all those strings, type this:

FOR i = 1 TO 7

print x$(i)

NEXT

That means: print all the strings in x$. The computer will print:

love

hate

kiss

kill

peace

war

why

That program includes a line saying x$(1) = "love". Instead of saying x$(1), math books say:

x1

The "1" is called a subscript.

Similarly, in the line saying x$(2) = "hate", the number 2 is a subscript. Some programmers pronounce that line as follows: "x string, subscripted by 2, is hate". Hurried programmers just say: "x string 2 is hate".

In that program, x$ is called an array (or matrix). Definition: an array (or matrix) is a variable that has subscripts.

Subscripted DATA

That program said x$(1) is "love", and x$(2) is "hate", and so on. This program does the same thing, more briefly:

CLS

DIM x$(7)

DATA love,hate,kiss,kill,peace,war,why

FOR i = 1 TO 7

READ x$(i)

NEXT

FOR i = 1 TO 7

PRINT x$(i)

NEXT

The DIM line says x$ will be a list of 7 strings. The DATA line contains a list of 7 strings. The first FOR..NEXT loop makes the computer READ those strings and call them x$. The bottom FOR...NEXT loop makes the computer print those 7 strings.

In that program, the first four lines say:

CLS

DIM

DATA

FOR i

Most practical programs begin with those four lines.

Let’s lengthen the program, so that the computer prints all this:

love

hate

kiss

kill

peace

war

why

why love

why hate

why kiss

why kill

why peace

why war

why why

That consists of two verses. The second verse resembles the first verse, except that each line of the second verse begins with "why".

To make the computer print all that, just add the shaded lines to the program:

CLS

DIM x$(7)

DATA love,hate,kiss,kill,peace,war,why

FOR i = 1 TO 7

READ x$(i)

NEXT

FOR i = 1 TO 7

PRINT x$(i)

NEXT

PRINT

FOR i = 1 TO 7

PRINT "why "; x$(i)

NEXT

The shaded PRINT line leaves a blank line between the first verse and the second verse. The shaded FOR..NEXT loop, which prints the second verse, resembles the FOR...NEXT loop that printed the first verse but prints "why" before each x$(i).

Let’s add a third verse, which prints the words in reverse order:

why

war

peace

kill

kiss

hate

love

Before printing that third verse, print a blank line:

PRINT

Then print the verse itself. To print the verse, you must print x$(7), then print x$(6), then print x$(5), etc. To do that, you could say:

PRINT x$(7)

PRINT x$(6)

PRINT x$(5)

etc.

But this way is shorter:

FOR i = 7 TO 1 STEP -1

PRINT x$(i)

NEXT

Numeric arrays

Let’s make y be this list of six numbers: 100, 26, 94, 201, 8.3, and -7. To begin, tell the computer that y will consist of six numbers:

CLS

DIM y(6)

Next, tell the computer what the six numbers are:

DATA 100,26,94,201,8.3,-7

Make the computer READ all that data:

FOR i = 1 TO 6

READ y(i)

NEXT

To make the computer PRINT all that data, type this:

FOR i = 1 TO 6

PRINT y(i)

NEXT

If you want the computer to add those 6 numbers together and print their sum, say:

PRINT y(1) + y(2) + y(3) + y(4) + y(5) + y(6)

Strange example

Getting tired of x and y? Then pick another letter! For example, you can play with z:

Silly, useless program What the program means

CLS CLear the Screen

DIM z(5) z will be a list of 5 numbers

FOR i = 2 TO 5

z(i) = i * 100 z(2)=200; z(3)=300; z(4)=400; z(5)=500

NEXT

z(1) = z(2) - 3 z(1) is 200 - 3, so z(1) is 197

z(3) = z(1) - 2 z(3) changes to 197 - 2, which is 195

FOR i = 1 TO 5

PRINT z(i) print z(1), z(2), z(3), z(4), and z(5)

NEXT

The computer will print:

197

200

195

400

500

Problems and solutions

Suppose you want to analyze 20 numbers. Begin your program by saying:

CLS

DIM x(20)

Then type the 20 numbers as data:

DATA etc.

Tell the computer to READ the data:

FOR i = 1 TO 20

READ x(i)

NEXT

Afterwards, do one of the following, depending on which problem you want to solve.…

Print all the values of x Solution:

FOR i = 1 TO 20

PRINT x(i)

NEXT

Print all the values of x, in reverse order Solution:

FOR i = 20 TO 1 STEP -1

PRINT x(i)

NEXT

Print the sum of all the values of x In other words, print x(1) + x(2) + x(3)+… + x(20). Solution: start the sum at 0 —

sum = 0

and then increase the sum, by adding each x(i) to it:

FOR i = 1 TO 20

sum = sum + x(i)

NEXT

Finally, print the sum:

PRINT "The sum of all the numbers is"; sum

Find the average of x In other words, find the average of the 20 numbers. Solution: begin by finding the sum —

sum = 0

FOR i = 1 TO 20

sum = sum + x(i)

NEXT

and then divide the sum by 20:

PRINT "The average is"; sum / 20

Find whether any of x’s values is 79.4 In other words, find out whether 79.4 is a number in the list. Solution: if x(i) is 79.4, print "Yes" —

FOR i = 1 TO 20

IF x(i)=79.4 THEN PRINT "Yes, 79.4 is in the list": END

NEXT

otherwise, print "No":

PRINT "No, 79.4 is not in the list"

In x’s list, count how often 79.4 appears Solution: start the counter at zero —

counter = 0

and increase the counter each time you see the number 79.4:

FOR i = 1 TO 20

IF x(i) = 79.4 THEN counter = counter + 1

NEXT

Finally, print the counter:

PRINT "The number 79.4 appears"; counter; "times"

 

Print all x values that are negative In other words, print all the numbers that have minus signs. Solution: begin by announcing your purpose —

PRINT "Here are the values that are negative:"

and then print the values that are negative; in other words, print each x(i) that’s less than 0:

FOR i = 1 TO 20

IF x(i) < 0 THEN PRINT x(i)

NEXT

Print all x values that are above average Solution: find the average —

sum = 0

FOR i = 1 TO 20

sum = sum + x(i)

NEXT

average = sum / 20

then announce your purpose:

PRINT "The following values are above average:"

Finally, print the values that are above average; in other words, print each x(i) that’s greater than average:

FOR i = 1 TO 20

IF x(i) > average THEN PRINT x(i)

NEXT

Find the biggest value of x In other words, find which of the 20 numbers is the biggest. Solution: begin by assuming that the biggest is the first number —

biggest = x(1)

but if you find another number that’s even bigger, change your idea of what the biggest is:

FOR i = 2 TO 20

IF x(i) > biggest THEN biggest = x(i)

140 NEXT

Afterwards, print the biggest:

PRINT "The biggest number in the list is"; biggest

Find the smallest value of x In other words, find which of the 20 numbers is the smallest. Solution: begin by assuming that the smallest is the the first number —

smallest = x(1)

but if you find another number that’s even smaller, change your idea of what the smallest is:

FOR i = 2 TO 20

IF x(i) < smallest THEN smallest = x(i)

NEXT

Afterwards, print the smallest:

PRINT "The smallest number in the list is"; smallest

Check whether x’s list is in strictly increasing order In other words, find out whether the following statement is true: x(1) is a smaller number than x(2), which is a smaller number than x(3), which is a smaller number than x(4), etc. Solution: if x(i) is not smaller than x(i + 1), print "No" —

FOR I = 1 TO 19

IF x(i) >= x(i + 1) THEN

PRINT "No, the list is not in strictly increasing order"

END

END IF

NEXT

otherwise, print "Yes":

PRINT "Yes, the list is in strictly increasing order"

Test yourself: look at those problems again, and see whether you can figure out the solutions without peeking at the answers.

Multiple arrays

Suppose your program involves three lists. Suppose the first list is called a$ and consists of 18 strings; the second list is called b and consists of 57 numbers; and the third list is called c$ and consists of just 3 strings. To say all that, begin your program with this statement:

DIM a$(18), b(57), c$(3)

Double subscripts

You can make x$ be a table of strings, like this:

"dog" "cat" "mouse"

X$=

"hotdog" "catsup" "mousetard"

Here’s how to make x$ be that table.…

Begin by saying:

CLS

DIM x$(2, 3)

That says x$ will be a table having 2 rows and 3 columns.

Then tell the computer what strings are in x$. Type these lines:

x$(1, 1) = "dog"

x$(1, 2) = "cat"

x$(1, 3) = "mouse"

x$(2, 1) = "hotdog"

x$(2, 2) = "catsup"

x$(2, 3) = "moustard"

That says the string in x$’s first row and first column is "dog", the string in x$’s first row and second column is "cat", etc.

If you’d like the computer to print all those strings, type this:

FOR i = 1 TO 2

FOR j = 1 TO 3

PRINT x$(i, j),

NEXT

PRINT

NEXT

That means: print all the strings in x$. The computer will print:

dog cat mouse

hotdog catsup mousetard

Most programmers follow this tradition: the row’s number is called i, and the column’s number is called j. That program obeys that tradition. The "FOR i = 1 TO 2" means "for both rows"; the "FOR j = 1 TO 3" means "for all 3 columns".

Notice i comes before j in the alphabet; i comes before j in x(i, j); and "FOR i" comes before "FOR j". If you follow the i-before-j tradition, you’ll make fewer errors.

At the end of the first PRINT line, the comma makes the computer print each column in a separate zone. The other PRINT line makes the computer press the ENTER key at the end of each row. The x$ is called a table or two-dimensional array or doubly subscripted array.

Multiplication table

This program prints a multiplication table:

CLS

DIM x(10, 4)

FOR i = 1 TO 10

FOR j = 1 TO 4

x(i, j) = i * j

NEXT

NEXT

FOR i = 1 TO 10

FOR j = 1 TO 4

PRINT x(i, j),

NEXT

PRINT

NEXT

Line 2 says x will be a table having 10 rows and 4 columns.

The line saying "x(i, j) = i * j" means the number in row i and column j is i*j. For example, the number in row 3 and column 4 is 12. Above that line, the program says "FOR i = 1 TO 10" and "FOR j = 1 TO 4", so that x(i,j)=i*j for every i and j, so every entry in the table is defined by multiplication.

The computer prints the whole table:

1 2 3 4

2 4 6 8

3 6 9 12

4 8 12 16

5 10 15 20

6 12 18 24

7 14 21 28

8 16 24 32

9 18 27 36

10 20 30 40

Instead of multiplication, you can have addition, subtraction, or division: just change the line saying "x(i, j) = i * j".

Summing a table

Suppose you want to analyze this table:

32.7 19.4 31.6 85.1

-8 402 -61 0

5106 -.2 0 -1.1

36.9 .04 1 11

777 666 55.44 2

1.99 2.99 3.99 4.99

50 40 30 20

12 21 12 21

0 1000 2 500

Since the table has 9 rows and 4 columns, begin your program by saying:

CLS

DIM x(9, 4)

Each row of the table becomes a row of the DATA:

DATA 32.7, 19.4, 31.6, 85.1

DATA -8, 402, -61, 0

DATA 5106, -.2, 0, -1.1

DATA 36.9, .04, 1, 11

DATA 777, 666, 55.44, 2

DATA 1.99, 2.99, 3.99, 4.99

DATA 50, 40, 30, 20

DATA 12, 21, 12, 21

DATA 0, 1000, 2, 500

Make the computer READ the data:

FOR i = 1 TO 9

FOR j = 1 TO 4

READ x(i, j)

NEXT

NEXT

 

To make the computer print the table, say this:

FOR i = 1 TO 9

FOR j = 1 TO 4:

PRINT X(I,J),

NEXT

PRINT

NEXT

Here are some problems, with solutions.…

Find the sum of all the numbers in the table Solution: start the sum at 0 —

sum = 0

and then increase the sum, by adding each x(i, j) to it:

FOR i = 1 TO 9

FOR j = 1 TO 4

sum = sum + x(i, j)

NEXT

NEXT

Finally, print the sum:

PRINT "The sum of all the numbers is"; sum

The computer will print:

The sum of all the numbers is 8877.84

Find the sum of each row In other words, make the computer print the sum of the numbers in the first row, then the sum of the numbers in the second row, then the sum of the numbers in the third row, etc. Solution: the general idea is —

FOR i = 1 TO 9

print the sum of row i

NEXT

Here are the details:

FOR i = 1 TO 9

sum = 0

FOR j = 1 TO 4

sum = sum + x(i, j)

NEXT

PRINT "The sum of row"; i; "is"; sum

NEXT

The computer will print:

The sum of row 1 is 168.8

The sum of row 2 is 333

The sum of row 3 is 5104.7

etc.

Find the sum of each column In other words, make the computer print the sum of the numbers in the first column, then the sum of the numbers in the second column, then the sum of the numbers in the third column, etc. Solution: the general idea is —

FOR j = 1 TO 4

print the sum of column j

NEXT

Here are the details:

FOR j = 1 TO 4

sum = 0

FOR i = 1 TO 9

sum = sum + x(i, j)

NEXT

PRINT "The sum of column"; j; "is"; sum

NEXT

The computer will print:

The sum of column 1 is 6008.59

The sum of column 2 is 2151.23

The sum of column 3 is 75.03

The sum of column 4 is 642.99

In all the other examples, "FOR i" came before "FOR j"; but in this unusual example, "FOR i" comes after "FOR j".

SUB procedures

Here’s a sick program:

CLS

PRINT "We all know..."

PRINT "You are stupid!"

PRINT "You are ugly!"

PRINT "...and yet we love you."

It makes the computer print this message:

We all know...

You are stupid!

You are ugly!

...and yet we love you.

So the computer prints "We all know...", then insults the human ("You are stupid! You are ugly!"), then prints "...and yet we love you."

Here’s a more sophisticated way to write that program:

CLS

PRINT "We all know..."

insult

PRINT "...and yet we love you."

SUB insult

PRINT "You are stupid!"

PRINT "You are ugly!"

END SUB

I’m going to explain that sophisticated version. Just read my explanation: don’t type the sophisticated version into your computer yet. (Wait until you read the next section, called "How to type the program".)

In the sophisticated version, the top 4 lines tell the computer to clear the screen (CLS), print "We all know...", then insult the human, then print "...and yet we love you." But the computer doesn’t know how to insult yet.

The bottom 4 lines teach the computer how to insult: they say "insult" means to print "You are stupid!" and "You are ugly!" Those bottom 4 lines define the word insult; they’re the definition of insult.

That program is divided into two procedures. The top 4 lines are called the main procedure (or main routine or main module). The bottom 4 lines (which just define the word "insult") are called the SUB procedure (or subroutine or submodule).

The SUB procedure’s first line (SUB insult) means: here’s the SUB procedure that defines the word "insult". The SUB procedure’s bottom line (END SUB) means: this is the END of the SUB procedure.

How to type the program

Now you’re smart enough to begin typing that sophisticated program! Begin by typing the first four lines. Then start typing the SUB procedure, beginning with the line that says "SUB insult".

When you finish typing the "SUB insult" line (and press the ENTER key at the end of that line), the computer analyzes that line and realizes you’re starting to type a new procedure. The computer devotes the entire screen to the new procedure. Yes, the screen shows just the SUB insult procedure! The screen no longer shows the main procedure! Here’s the rule: the computer’s screen shows just one procedure at a time.

So now the top of the screen says "SUB insult". At the bottom of the screen, the computer automatically types "SUB END" for you. In between the "SUB insult" and "SUB END" lines, type PRINT "You are stupid!" and PRINT "You are ugly!" (and indent those lines by pressing the TAB key), so the screen looks like this:

SUB insult

PRINT "You are stupid!"

PRINT "You are ugly!"

END SUB

Congratulations! You finished typing the program!

Seeing different procedures

The computer’s screen shows just the SUB procedure. To see the main procedure instead, press the F2 key, then ENTER.

To flip back to the SUB procedure again, press the F2 key again, then the down-arrow key (so the world "insult" is highlighted), then ENTER.

Here’s the rule: to see a different procedure, press the F2 key, then highlight the name of the procedure you want to see (by pressing the down-arrow key if necessary), then press ENTER.

Run

Whenever you want to run the program, press SHIFT with F5. The computer will say:

We all know...

You are stupid!

You are ugly!

...and yet we love you.

Print

If you choose Print from the file menu (by pressing Alt then F then P) and then press ENTER, the computer will print the entire program onto a single sheet of paper.

When printing on paper, the computer will automatically leave a blank line between the procedures, so the paper will show this:

CLS

PRINT "We all know..."

insult

PRINT "...and yet we love you."

SUB insult

PRINT "You are stupid!"

PRINT "You are ugly!"

END SUB

You must eject the paper from the printer manually.

Save

If you choose Save from the file menu (by pressing Alt then F then S) and then give the program a name (and press ENTER), the computer saves the entire program onto the hard disk.

While saving, the computer automatically adds an extra line at the top of the program, so the main procedure becomes this:

DECLARE SUB insult ()

CLS

PRINT "We all know..."

insult

PRINT "...and yet we love you."

The DECLARE line reminds the computer that the program includes a SUB insult.

Refrains

This is chanted by boys playing tag — and protesters fearing dictators:

The lion is a-coming near.

He'll growl and sneer

And drink our beer.

The lion never brings us cheer.

He'll growl and sneer

And drink our beer.

The lion is the one we fear.

He'll growl and sneer

And drink our beer.

Gotta stop the lion!

In that chant, this refrain is repeated:

He'll growl and sneer

And drink our beer.

This program prints the entire chant:

CLS

PRINT "The lion is a-coming near."

refrain

PRINT "The lion never brings us cheer."

refrain

PRINT "The lion is the one we fear."

refrain

PRINT "Gotta stop the lion!"

SUB refrain

PRINT " He'll growl and sneer"

PRINT " And drink our beer."

END SUB

Young males Here’s a poem about young male relationships:

He is a boy--

A little boy.

Don't play with me.

I'm not a toy.

His only goal

Is to annoy.

Don't play with me.

I'm not a toy.

His life is full

Of painful joy.

Don't play with me

I'm not a toy.

He's just a boy.

This program prints it:

CLS

PRINT "He is a boy--"

PRINT "A little boy."

refrain

PRINT "His only goal"

PRINT "Is to annoy."

refrain

PRINT "His life is full"

PRINT "Of painful joy."

refrain

PRINT "He's just a boy."

SUB refrain

PRINT " Don't play with me."

PRINT " I'm not a toy."

END SUB

Clementine The famous folk song "Clementine" begins like this:

In a cavern in a canyon, excavating for a mine,

Lived a miner (49'er) and his daughter, Clementine.

O my darling, o my darling, o my darling Clementine,

You are lost and gone forever. Dreadful sorry, Clementine!

Light she was and like a fairy, and her shoes were #9.

Herring boxes without tops: those sandals were for Clementine.

O my darling, o my darling, o my darling Clementine,

You are lost and gone forever. Dreadful sorry, Clementine!

Drove her ducklings to the water ev'ry morning just at 9.

Hit her foot against a splinter, fell into the foaming brine.

O my darling, o my darling, o my darling Clementine,

You are lost and gone forever. Dreadful sorry, Clementine!

This program prints the song’s updated version with a twisted ending:

CLS

PRINT "In a cavern in a canyon, excavating for a mine,"

PRINT "Lived a miner (49'er) and his daughter, Clementine."

chorus

PRINT "Light she was and like a fairy, and her shoes were #9."

PRINT "Herring box-es without tops-es sandals were for Clementine."

chorus

PRINT "Drove her ducklings to the water ev'ry morning just at 9."

PRINT "Hit her foot against a splinter, fell into the foaming brine."

chorus

PRINT "Ruby lips above the water, blowing bubbles soft and fine!"

PRINT "But alas, I was no swimmer, so I lost my Clementine."

chorus

PRINT "How I missed her! How I missed her! How I missed my Clementine!"

PRINT "But I kissed her little sister and forgot my Clementine."

chorus

PRINT "Sister gladly to me married. Then she found in nine months time"

PRINT "A nice daughter. As she oughta, named the daughter Clementine."

chorus

PRINT "There's our daughter in the water. Suddenly, she gives a wail"

PRINT "At some red-stained herring boxes. Now I'm sitting here in jail."

chorus

PRINT "In my dreams she still doth haunt me, robed in garments soaked in brine."

PRINT "Once I wooed her. Now a loser singing songs while doing time!"

chorus

SUB chorus

SLEEP 11

PRINT

PRINT " O my darling, o my darling, o my darling Clementine,"

PRINT " You are lost and gone forever. Dreadful sorry, Clementine!"

SLEEP 11

PRINT

END SUB

At the beginning and end of the chorus, the "SLEEP 11" makes the computer pause for 11 seconds, to give the human a chance to read & sing what the computer wrote before the computer puts more words onto the screen.

Big love

This program prints a love poem:

CLS

PRINT "The most beautiful thing in the world is"

PRINT "LOVE"

PRINT "The opposite of war is"

PRINT "LOVE"

PRINT "And when I look at you, I feel lots of"

PRINT "LOVE"

In that program, many of the lines make the computer print the word LOVE. Let’s make those lines print the word LOVE bigger, like this:

* * * * * * * * *

* * * * * *

* * * * * * * *

* * * * * *

* * * * * * * * * * * *

To make LOVE be that big, run this version of the program:

CLS

PRINT "The most beautiful thing in the world is"

big.love

PRINT "The opposite of war is"

big.love

PRINT "And when I look at you, I feel lots of"

big.love

SUB big.love

PRINT "* * * * * * * * *"

PRINT "* * * * * *"

PRINT "* * * * * * * *"

PRINT "* * * * * *"

PRINT "* * * * * * * * * * * *"

END SUB

In that version, the lines say "big.love" instead of PRINT "LOVE". The SUB procedure teaches the computer how to make big.love.

Variables

Each procedure uses its own part of the RAM. For example, the main procedure uses a different part of the RAM than a SUB procedure.

Suppose the main procedure says "x = 4", and a SUB procedure named "joe" says "x = 100". The computer puts 4 into the main procedure’s x box and puts 100 into joe’s x box, like this:

┌─────────────┐

main procedure's x box │ 4 │

└─────────────┘

┌─────────────┐

joe's x box │ 100 │

└─────────────┘

Those two boxes are stored in different parts of the RAM from each other, and they don’t interfere with each other.

For example, suppose you run this program:

CLS

x = 4

joe

PRINT x

SUB joe

PRINT x

x = 100

END SUB

The computer begins by doing the main procedure, which says "x = 4", so the computer puts 4 into the main procedure’s x box:

┌─────────────┐

main procedure's x box │ 4 │

└─────────────┘

The main procedure’s next line says "joe", which makes the computer do the joe procedure. The joe procedure begins by saying "PRINT x"; but since joe’s x box is still empty, the computer will print 0. Joe’s next line says "x = 100", which puts 100 into joe’s x box. Then the computer comes to the end of joe, returns to the main procedure, and does the "PRINT x" line at the bottom of the main procedure; but since the main procedure’s x box still contains 4, the computer will print 4. The computer will not print 100.

If a committee of programmers wants to write a big, fancy program, the committee divides the programming task into a main procedure and several SUB procedures, then assigns each procedure to a different programmer. If you’re one of the programmers, you can use any variable names you wish, without worrying about what names the other programmers chose: if you accidentally pick the same variable name as another programmer, it’s no problem, since each procedure stores its variables in a different part of the RAM.

If you want a variable to affect and be affected by what’s in another procedure, use one of these methods…

Method 1: SHARED At the top of the main procedure, you can say:

COMMON SHARED x

That means x is a variable whose box will be shared among all procedures, so that if a procedure says "x = 4" the x will be 4 in all procedures.

For example, suppose you say:

COMMON SHARED x

CLS

x = 4

joe

PRINT x

SUB joe

PRINT x

x = 100

END SUB

Then when the computer comes to joe’s first line, which says "PRINT x", the computer will print 4 (because the main procedure had made x become 4); and when the computer comes to the main procedure’s bottom line, which says "PRINT x", the computer will print 100 (because the joe procedure had made x become 100).

Put the COMMON SHARED line at the main procedure’s top, above CLS.

You can write a program containing other shared variables besides x. For example, if you want x and sammy$ to both be common shared variables, say:

COMMON SHARED x, sammy$

If you want y$ to be a list of 20 strings, you normally say DIM y$(20); but if you want to share that list among all the procedures, say this instead:

DIM SHARED y$(20)

Put that line just at the top of the main procedure; you do not need to say DIM y$(20) in the SUB procedures.

The program is your world! A SHARED variable is called a global variable, since its value is shared throughout the entire program. An ordinary, unshared variable is called a local variable, since its value is used just in one procedure.

Method 2: arguments Here’s a simple program:

CLS

INPUT "How many times do you want to kiss"; n

FOR i = 1 TO n

PRINT "kiss"

NEXT

It asks "How many times do you want to kiss", then waits for your answer, then prints the word "kiss" as many times as you requested. For example, if you type 3, the computer will print:

kiss

kiss

kiss

If you input 5 instead, the computer will print this instead:

kiss

kiss

kiss

kiss

kiss

Let’s turn that program into a SUB procedure that gets its input from the main procedure instead of from a human. Here’s the SUB procedure:

SUB kiss (n)

FOR i = 1 TO n

PRINT "kiss"

NEXT

END SUB

In that SUB procedure’s top line, the "(n)" means "input the number n from the main procedure, instead of from a human". If the main procedure says —

kiss 3

then the n will be 3, so the SUB procedure will print "kiss" 3 times, like this:

kiss

kiss

kiss

If the main procedure says —

kiss 5

then the n will be 5, so the SUB procedure will print "kiss" 5 times.

Please type this complete program, which contains that SUB procedure:

DEFINT A-Z

CLS

PRINT "The boy said:"

kiss 3

PRINT "His girlfriend said okay!"

PRINT "Then the boy said:"

kiss 5

PRINT "His girlfiend said okay!"

PRINT "Finally, the boy said:"

kiss 8

PRINT "His girlfriend said:"

PRINT "I'm not prepared to go that far."

SUB kiss (n)

FOR i = 1 TO n

PRINT "kiss"

NEXT

END SUB

When you run that program, the computer will print:

The boy said:

kiss

kiss

kiss

His girlfriend said okay!

Then the boy said:

kiss

kiss

kiss

kiss

kiss

His girlfriend said okay!

Finally, the boy said:

kiss

kiss

kiss

kiss

kiss

kiss

kiss

kiss

His girlfriend said:

I'm not prepared to go that far.

In that SUB procedure’s top line, the n is called the parameter; put it in parentheses. In the line that says "kiss 3", the 3 is called the argument.

In that program, instead of saying —

kiss 3

you can say:

y = 3

kiss y

Then y’s value (3) will become n, so the SUB procedure will print "kiss" 3 times. The n (which is the parameter) will use the same box as y (which is the argument). For example, if you insert into the SUB procedure a line saying "n = 9", the y will become 9 also.

You can write fancier programs. Here’s how to begin a SUB procedure called joe having three parameters (n, m, and k$):

SUB joe (n, m, k$)

To use that subroutine, give a command such as:

joe 7, 9, "love"

Suppose your main procedure says:

DIM x$(3)

x$(1) = "love"

x$(2) = "death"

x$(3) = "war"

That means x$ is a list of these 3 strings: "love", "death", and "war". To make joan be a SUB procedure manipulating that list, make joan’s top line say —

SUB joan (x$())

In that line, the () warns the computer that x$ is a list. You do not need to say DIM x$(3) in the SUB procedure. When you want to make the main procedure use joan, put this line into the main procedure:

joan x$()

Those lines, "SUB joan (x$())" and "joan x$()", work even if x$ is a table defined by a line such as DIM x$(30, 40).

DEFINT To make all the variables in your program be short integers, say "DEFINT A-Z" at the top of the main procedure and say it again at the top of each SUB procedure, so SUB procedure joe begins like this:

DEFINT A-Z

SUB joe

If you typed "DEFINT A-Z" at the top of the main procedure, the computer will automatically type "DEFINT A-Z" for you at the top of each new SUB procedure.