Applesoft BASIC Programming Course

This fast‑paced BASIC programming course, written for the Apple II computer, was created with children in mind (especially my son), but grown‑ups can enjoy it too. BASIC is a wonderful language for your first programming adventure — simple, clear, and using only uppercase letters, which makes learning easier for younger coders. The version for Apple II feels much more intuitive than versions available for other 8‑bit platforms. It also offers a very easy way to do graphics.

Every lesson contains material that will be used in later stages of the course, so take your time to read carefully and don’t move on until you’ve mastered what came before. The names of functions used in Applesoft BASIC are highlighted in uppercase, bold and this colour. Part three of the course is designed for older children and grown‑ups.

Part 1  Basics

Computer or emulator?

Apple II is the longest‑produced personal computer in history – production lasted over 16 years. You can find working Apple II computers on eBay, but here we’ll focus on emulators available for all major platforms: AppleWin for Windows, Virtual ][ for macOS, LinApple for Linux (it’s included in standard system repositories) and DinosPi, a project that compiles and configures the LinApple emulator for Raspberry Pi.


 

Lesson 1  First programs

When you turn on an Apple II computer, you see a screen with a closing square bracket and a blinking cursor. Right now the machine is waiting for your instructions.

Type the following code:

PRINT "HELLO WORLD!"

and press ENTER.

The computer will display the text HELLO WORLD!, which is the most popular first program. The second is:

PRINT 2+2

The computer will show the result of the calculation, which is the digit 4.


 

Lesson 2  Understanding line numbers

Line numbers tell the computer in what order to execute your commands. A command numbered 10 will be executed before a command numbered 20. Try writing a slightly more complex program that displays more than one line of text. Enter:

10 PRINT "HELLO WORLD!"
20 PRINT "HOW ARE YOU?"
30 END

To run it, type RUN and press ENTER. When the program runs, the computer will display two lines of text one after the other.

The END command tells the computer that we’ve finished typing the program.

If you start writing another line using a line number that already exists, the previous line in memory will be overwritten and replaced with the new content.

If you need to insert extra code between existing lines, just choose a line number between those already used. In the above case you can add at the bottom of the screen:

15 PRINT "BEAUTIFUL WEATHER!"

You can clear the screen with the HOME command.

To re‑list the entire code, already with sorted lines, use the LIST command.

You can create a new program with the NEW command – this removes the entire previous program from memory.

Practise these new commands.


 

Lesson 3  The GOTO function

The GOTO instruction lets us jump to another part of our program. We use it with the line number we want to jump to.

You can use GOTO for different purposes — write the program below and run it:

10 PRINT "HELLO!"
20 GOTO 40
30 PRINT "YOU WON'T SEE ME :-("
40 PRINT "AND WELCOME AGAIN!"
50 END

In this program, line 20 tells the computer to jump to line 40. That’s why line 30 will never be executed, and on the screen you will see only HELLO! AND WELCOME AGAIN!.

Here’s another example of a popular program for 8‑bit computers that uses the GOTO function:

10 PRINT "HELLO WORLD"
20 GOTO 10

In this program, line 20 tells the computer to jump back to line 10. The program runs continuously until you press CTRL+C, so it doesn’t need the END instruction.


 

Lesson 4  Editing code

As our code grows longer, let’s learn how to edit it so we don’t have to retype whole lines when we want to change just one character. Let’s edit line 10 from the previous lesson’s program so that the line no longer contains the exclamation mark. On Apple II computers, editing a listed program works like this:

  1. when the cursor is under the listed program, press the ESC key,
  2. move the cursor to line 10, placing it on the first character of that line (the digit 1) and press ESC again,
  3. from now on you can move along the line with the left and right arrow keys and freely edit the content — use the space bar to remove the exclamation mark,
  4. after finishing the editing, move the cursor with the arrow keys to the end of the line and press ENTER — the edited line has now been remembered by the computer,
  5. you can run the program by typing RUN or clear the screen and list the program again in full, showing your change.

 

Lesson 5  Saving and loading programs

The SAVE instruction is used to save your program to disk so you can come back to it later. Here’s how to use it:

10 PRINT "THIS IS A SHORT PROGRAM"
20 END
SAVE MYPROGRAM

After you execute SAVE, this short program is saved to disk under the name MYPROGRAM.

Use NEW now to clear that program from memory and check it’s gone with LIST.

The LOAD command loads a program from disk. Here’s how it works:

LOAD MYPROGRAM

This command loads from disk a program named MYPROGRAM. Check that it loaded correctly using LIST.

You can list a specific line number by giving it after the LIST command. You can also list a range of lines by specifying a start and end separated by a hyphen, for example LIST 30-50. Leaving the hyphen at the beginning or end means you want to list from the start or to the end.

The CATALOG command displays a list of all available programs. You can use it to see what programs you have saved on disk.

You’ve learned how to save, load and display files on disk. Remember to always save your programs so you can come back to them later.


 

Lesson 6  Adding comments

When you write a program it’s helpful to add comments (notes) for yourself or for other people who may read your code. In AppleSoft BASIC we can add a comment using the REM keyword, which stands for REMark.

For example:

10 REM THIS IS MY FIRST PROGRAM
20 PRINT "HELLO WORLD!"
30 PRINT "HOW ARE YOU?"
40 END

When the computer sees REM it knows that what comes after it is just a comment and doesn’t try to execute it. Comments in code are important because they let us more easily understand someone else’s code, or even our own after a longer break.

Write a new comment on a new line starting with the number 25. The content of the comment should be QUESTION, then list the entire code using the LIST command. Can you see how the code has sorted itself?

If you want to remove a comment or any other line, simply type the line number you want to remove without adding anything else. The comment or line will just disappear from the listing and will no longer be executed.


 

Lesson 7  Simple math

Computers are pretty good at counting! Write a program that performs the following mathematical operations:

10 PRINT 5 * 2 + 4 / 2 - 2
20 PRINT 5 * ( 2 + 4 / 2 ) - 2
30 END

When you run this program, the computer will display the first result 10 because it performs calculations according to the order of operations: first multiplication *, then division /, then addition +, and finally subtraction –. The second result will be 18 because calculations inside parentheses are given priority.


 

Lesson 8  Numeric variables

Variables are places where we can store information in our program. Think of them like boxes in which we can put different things. To create a variable, we simply write its name and then use the equals sign = to assign it a value. For example:

10 MYNUMBER = 5
20 PRINT MYNUMBER + 2
30 END

If you run this program, you’ll see it displays the digit 7. In line 10 we told the computer to put the digit 5 in the box named MYNUMBER, and then in line 20 we told it to add the digit 2 to whatever is in the MYNUMBER box.

The name of a variable can be any string of characters from A to Z, from one up to even more than 20 characters, including digits, but the name must start with a letter.

Remember, however, that the Applesoft BASIC interpreter uses only the first two characters of a variable name, so from the example above we get the digit 5 whether we reference the variable MYNUMBER or the variable MY. Remember this rule: any new variable whose name begins with the same first two characters as an existing variable will overwrite the value of that previous one. For this reason I recommend using only two‑character variable names in Applesoft BASIC.


 

Lesson 9  Text variables

Variables can store not only numbers but also text! The values of such variables are called character strings, or strings. To create a string we must place the text in quotation marks and end the variable name with a $. For example:

10 NAME$ = "ANN"
20 PRINT "HELLO " + NAME$
30 END

We use the variable NAME$ instead of NAME to tell the computer that this variable is a string (a sequence of characters) rather than a number. If you run this program, you’ll see it displays the text HELLO ANN. In line 10 we told the computer to put ANN into a box named NAME$, and then in line 20 we told it to display HELLO followed by a space, plus whatever is inside the NAME$ box.

Sometimes you will see the LET keyword before the variable name. It isn’t required, but it slightly increases the readability of the code. For our needs we’ll stick to automatic variable declaration, i.e. without using LET.


 

Lesson 10  Asking for information

We can also ask someone using our program for additional information and then use it later on. We’ll use the INPUT command. For example:

10 INPUT "WHAT IS YOUR NAME? "; NAME$
20 PRINT "HELLO " + NAME$
30 END

If you run this program, the computer will ask WHAT IS YOUR NAME?, and then it will display HELLO plus whatever you typed in answer to the question, because earlier the program placed the name you entered into the NAME$ box.


 

Lesson 11  The FOR loop

Loops are a very important part of programming. A FOR loop lets us perform a certain action many times. For example, we can write a program that counts from 1 to 10. Here’s how we do it:

10 FOR I = 1 TO 10
20   PRINT I
30 NEXT I
40 END

In this program, I is a variable whose value changes each time we go through the loop. It starts at 1 (FOR I = 1) and ends at 10 (TO 10). Each time we pass through the loop we display the value of I and then return to the beginning (NEXT I), increasing the value of I by 1. NEXT means “next”.


 

Lesson 12  The IF condition

Sometimes we want our program to do something only when a certain condition is met. For example, we might want the program to display HELLO ANNA only when the entered name is ANNA. The IF function comes to the rescue. Here’s how to use it:

10 INPUT "WHAT IS YOUR NAME? "; NAME$
20 IF NAME$ = "ANNA" THEN PRINT "HELLO ANNA"
30 END

If you run this program and enter ANNA, the computer will display HELLO ANNA. But if you enter anything else, nothing will be displayed.

Programming task #1
Write a simple, short text RPG with a choice of one of two directions for the player’s path, using the commands you’ve learned so far, and then save it under the name RPG.


 

Lesson 13  Randomness with RND

Computers can also generate random numbers! This is very useful, for example, when creating games. To generate a random number we use the RND function. Example:

10 HOME
20 FOR I = 1 TO 60
30   X = INT(RND(1) * 100) + 1
40   PRINT X,
50 NEXT I
60 END

This program displays 60 random numbers between 1 and 100. In line 30 the RND(1) function generates a random floating‑point number between 0 and 1. We multiply it by 100 to get a number between 0 and 99, and then we add 1 so the number falls between 1 and 100. The INT function rounds the number down to the nearest integer. In line 40 we use the PRINT function in an interesting way: we add a comma at the end of the variable name, which makes the random numbers appear in three equal columns.

Now you know how to use loops, conditions and randomness in AppleSoft BASIC.

Programming task #2
Expand your RPG game with a random element.


 

Lesson 14  Logical operators

In Applesoft BASIC, conditions in the IF instruction can be combined using the logical operators AND (and), OR (or) and NOT (not). Example:

10 FOR X = 1 TO 2
20   IF X > 5 OR X = 2 THEN PRINT "FOR X=";X;" THE OR CONDITION IS TRUE"
30   IF X <> 2 AND X < 3 THEN PRINT "X=";X;" IS LESS THAN 2"
40 NEXT X
50 END

The available comparison operators are:
= equal   <> not equal   < less than   <= less than or equal   > greater than   >= greater than or equal.

In the first output of the program the second condition will be satisfied, and in the second the first condition.

Fun fact: when you combine IF with the GOTO function, you don’t need to use THEN.


 

Lesson 15  A text game

Below is the code for a computer game that was popular in the 1970s, written in BASIC.

Rules of the game: the program draws one number from 1 to 100 and asks the player to guess it. The player has 7 tries, and the computer only tells whether the drawn number is bigger, smaller, or whether the player guessed it.

Below is the listing of the game code. Type it into the computer, save it for later and play:

10 PRINT "WELCOME TO THE GUESS THE NUMBER GAME!"
20 PRINT "I'M PICKING A NUMBER FROM 1 TO 100..."
30 TG = INT(RND(1) * 100) + 1
40 PRINT "PICKING COMPLETE. START GUESSING! YOU HAVE 7 TRIES."
45 REM CURRENT TRY NUMBER
50 TR = 1
60 INPUT "YOUR GUESS: "; GS
70 IF GS < TG THEN PRINT "TOO SMALL!": TR = TR + 1: GOTO 90 
80 IF GS > TG THEN PRINT "TOO LARGE!": TR = TR + 1: GOTO 90
90 IF TR = 7 THEN PRINT "YOU DIDN'T GUESS IT! THE NUMBER WAS: "; TG: GOTO 130
100 IF GS = TG THEN PRINT "CONGRATULATIONS, YOU GUESSED IT!": GOTO 130
110 GOTO 60
130 INPUT "DO YOU WANT TO PLAY AGAIN? (Y/N) "; AN$
140 IF AN$="Y" THEN GOTO 10
150 PRINT "THANK YOU FOR PLAYING!"
160 END

Programming task #6 – expand the code so that it also displays the “current attempt number” in the format 1/7, 3/7 and so on up to 7/7.


 

Lesson 16  Reading data with DATA

The DATA function lets us store data inside our program. We can then read these data using the READ command. Here’s an example:

10 DATA 5, 10, 15
20 FOR I = 1 TO 3
30   READ X
40   PRINT X
50 NEXT I
60 END

In this program we have three values stored in DATA: 5, 10 and 15. The FOR loop reads each of these values (READ X) and displays it on the screen. The RESTORE function restores the ability to read the data from the beginning.


 

Lesson 17  Nesting loops

Loops can be placed inside other loops. We call this nesting loops. Below is an example that displays all the combinations of a popular combination lock known from suitcases and briefcases:

10 REM COMBINATION LOCK COMBINATIONS
20 FOR I = 0 TO 9
30   FOR J = 0 TO 9
40     FOR K = 0 TO 9
50       PRINT I; J; K,
60     NEXT K
70   NEXT J
80 NEXT I
90 END

In this program we have a FOR loop inside another loop and inside yet another loop. For each value of I, the loop with variable J is executed ten times, and likewise for the value K. This gives us a total of 1 000 different combinations of I, J, K that are displayed on the screen.

Adding a semicolon in line 50 means that after the PRINT function no new line character will be inserted. Adding a comma, as in the lesson on random numbers, creates three equal columns with the results.

We use larger indents (spaces after the line number) to visually create a block of loops, which makes the code more readable. However, when you save the program on an Apple II computer, these spaces will be removed.

If you finish the innermost loop with the NEXT instruction (in the reverse order of nesting), you don’t need to indicate the name of the variable you are operating on, so lines 60 to 80 can be replaced by a single line:

60 NEXT : NEXT : NEXT

This way the K loop will be ended first, then J, and finally I.


 

Lesson 18  Controlling text

Sometimes we’d like to have better control over text. For example, we might want to centre it or add various decorations. Use the following code to get familiar with functions that help achieve this:

10 REM COVER
20 HOME
30 VTAB 8 : HTAB 15
40 PRINT "MARK TWAIN"
50 VTAB 12 : HTAB 8
60 INVERSE
70 PRINT TAB(32)
85 VTAB 13 : HTAB 8
90 PRINT " THE ADVENTURES OF TOM SAWYER "
100 VTAB 14 : HTAB 8
110 PRINT TAB(32)
120 NORMAL
130 VTAB 18 : HTAB 18
140 FLASH
150 PRINT "1876"
160 NORMAL
170 VTAB 2

The VTAB function moves the cursor to a specific line, and HTAB moves it to a specific column so you can start writing text from there. The INVERSE function reverses the colours, the FLASH function makes the text blink, the NORMAL function returns normal colours and turns off blinking, and the TAB function automatically inserts as many spaces as the number passed as a parameter. The final result:


 

Lesson 19  Arrays

Arrays are a very useful tool. You can imagine them as a row of boxes in which we can store different values. Let’s see how we can use them:

10 DIM TBL(5)
20 FOR I = 1 TO 5
30   TBL(I) = I * I
40 NEXT I
50 FOR I = 1 TO 5
60   PRINT "THE SQUARE OF ";I;" IS ";TBL(I)
70 NEXT I
80 END

In this program the DIM function creates an array named TBL with five places to store values. Lines 20 to 40 fill the array with the squares of numbers from 1 to 5 (that is, the program multiplies the number I by itself). Lines 50 to 70 display these values on the screen.


 

Part 2  Drawing

Applesoft BASIC has several graphics modes. Each of them has its advantages and disadvantages. The first, popular low‑resolution mode is GR, which allows drawing at a resolution of only 40 × 40 pixels but uses as many as sixteen colours (from 0 to 15). This mode leaves two lines of text available at the bottom of the screen. The second mode is high‑resolution HGR, which allows the use of only six colours (from 0 to 5) but offers a resolution of 280 × 160 pixels, again leaving two lines of text at the bottom of the screen. The full‑screen high‑resolution mode is HGR2; its resolution is 280 × 192 pixels, and the number of colours is still just six.

 

Lesson 20 Drawing in low resolution

We’ll start with GR mode. With the code below you’ll draw a few large points on the screen. The PLOT function draws a point using the coordinates X and Y. The first number after PLOT tells how many pixels from the left edge of the screen the point should be, and the second number tells how many pixels from the top of the screen. If you switch to a colour screen, each of the points, after running the program below, will be in a different colour – thanks to the COLOR function.

10 GR
20 FOR I = 14 TO 24 STEP 5
30   COLOR = I - 2
40   PLOT I,19
50 NEXT I
60 END

You can continue drawing points by typing the PLOT command without line numbers and writing in the last two lines of the editor. The editor is now switched to graphics mode and allows you to write code only in the last two lines. To restore the ability to write code on the whole screen, enter TEXT:HOME. To return to graphics mode and writing in the last two lines, enter GR.

In the program above we used the STEP option in the loop, which indicates how many steps the value of the loop variable should be increased by. In this case the loop goes in steps of five, that is 14, 19, 24. Thanks to this the three points are displayed exactly in the centre of the screen with larger gaps between them. We use the same numbers to change the colour, but the most popular colour values are obtained by subtracting 2 from each of them. The final result:


 

Lesson 21  A simple mosaic

Let’s use our newfound knowledge and draw a simple mosaic using the PLOT function.

10 REM POINT MOSAIC
20 GR: COLOR=3 : HOME
30 XC = 19: YC = 20: D = 1
40 FOR S = 1 TO 38: FOR I = 1 TO S
50   PLOT XC, YC
60   IF D = 1 THEN XC = XC + 1
70   IF D = 2 THEN YC = YC + 1
80   IF D = 3 THEN XC = XC - 1
90   IF D = 4 THEN YC = YC - 1
100   NEXT I
110  D = D + 1: IF D > 4 THEN D = 1
120 NEXT S
130 END

In line 10 we define GR mode, choose pink as the active drawing colour, and clear the screen. Then the loop draws a point according to the current coordinates and performs the necessary calculations to determine the next mosaic points. The final result looks like this:

Because the program draws point by point, it takes quite a while to run. Below is a faster version that draws using the HLIN, VLIN and AT functions to create lines instead of points. This makes the program much faster:

10 REM LINE MOSAIC
20 GR: COLOR=3 : HOME
30 X = 19: Y = 20: D = 1
40 FOR S = 1 TO 37
50   IF D = 1 THEN HLIN X, X + S AT Y: X = X + S
60   IF D = 2 THEN VLIN Y, Y + S AT X: Y = Y + S
70   IF D = 3 THEN HLIN X - S, X AT Y: X = X - S
80   IF D = 4 THEN VLIN Y - S, Y AT X: Y = Y - S
90   D = D + 1
100  IF D > 4 THEN D = 1
110 NEXT S
120 END

As the example shows, the construction for drawing lines with these functions is as follows: HLIN FROM, TO AT ROW‑NUMBER for horizontal lines and VLIN FROM, TO AT COLUMN‑NUMBER for vertical lines.

The finished drawing:

 


 

Lesson 22 Drawing in high resolution

In high‑resolution mode and using the HPLOT function you will draw much smaller points on the screen. The name of the function that sets the colour has also changed – now it is HCOLOR. Both functions are used in the same way as in GR mode.

10 HGR
20 HCOLOR = 1 : HPLOT 50,50
30 HCOLOR = 5 : HPLOT 100,100
40 HCOLOR = 6 : HPLOT 150,150
50 END

As you can see, here the points are much smaller than in GR mode and you can properly call them pixels.

Programming task #3 – create a program that, using a loop and drawing point by point, draws a simple line across the screen.


 

Lesson 23  A sine wave

The SIN function calculates the sine of an angle, and the INT function turns a number into an integer. Using these functions we can easily draw a sine wave:

10 HGR2
20 FOR X = 1 TO 275
30   Y = INT (SIN (X / 40) * 75) + 75
40   HPLOT X,Y
50 NEXT X
60 END

HGR2 is a graphics mode that fills the entire screen — however, if after the program ends you still don’t see a cursor, typing TEXT and pressing ENTER will return you to text mode. Below is the graph produced by the program:

Programming task #4 – the effect of the above code is a simplified sine wave – make your version look like the graphic above. The vertical thickness of the sine line should be 3 pixels and somewhere on the screen you should display the current value of SIN(X), using the lesson on controlling the position of text.


 

Lesson 24  Drawing lines

We draw lines using the same command as in previous lessons. However, we add a second set of coordinates after the word TO. The line will be drawn from the coordinates of the first point to the coordinates of the second point.

HGR : HPLOT 0,0 TO 130,130

Below is my little program that draws a pentagram projection using this function – we’ll use a base of coordinates created with the DATA function we learned earlier. The program is also interesting because it draws from point to point, so in DATA you only need to place the coordinates of the successive points, and the program itself assigns them as X1,Y1 TO X2,Y2. The code also contains an example of using two operations separated by a colon as the result of an IF. Adding a colon separates instructions just as if you had added another line number, yet still staying on the same line.

10 REM PENTAGRAM
20 DATA 130,30,205,134,70,64
30 DATA 190,64,55,134,130,30
40 HOME : HGR
50 X = 0 : Y = 0
60  FOR I = 0 TO 5
70    READ A,B
80    IF I = 0 THEN X = A : Y = B : NEXT I
90    HPLOT X,Y TO A,B
100   X = A : Y = B
110   FOR U = 1 TO 500 : NEXT U
120 NEXT I
130 END

In line 70 we use the READ function to read two values from the DATA base at once, assigning them immediately to two variables. Line 110 is a loop that emulates a SLEEP function, which doesn’t exist on Apple II, making the drawing appear as if it were drawn by a human hand, much more slowly.

You can use the above program to create any drawing consisting of straight lines by appropriately filling in the DATA values.

Imagine what interesting drawings you can create by combining it with the random RND function.


 

Lesson 25 Randomness and drawing, or a simple screen saver

Below is a very simple screen saver that draws random lines, clears the screen and then starts again. Copy it and run it, then later do the programming task.

    10  REM SCREENSAVER
    20  DIM TB(5)
    30  HOME : HGR2
    40  FOR I = 1 TO 50
    50    FOR K = 1 TO 4
    60      TB(K) = INT ( RND (1) * 190) + 1
    70    NEXT K
    80    HCOLOR = INT ( RND (1) * 6) + 1
    90    HPLOT TB(1),TB(2) TO TB(3),TB(4)
    100 NEXT I : GOTO 30

For simplicity the screen saver draws lines only in a 190 × 190 pixel square, leaving an empty margin on the right. Your next, fifth programming task is to expand the code so that the lines are drawn across the entire available screen area.


 

Lesson 26 Fancy drawing

Below is a popular, simple and interesting graphic effect known from 8‑bit computers:

Analyse the code to understand how the effect was achieved.

10  REM FANCY EFFECT
20  HOME : HGR2 : SL = 7
30  V = 279 : N = 189
40  FOR I = 0 TO 1 STEP 0.05
50    HCOLOR = 6
60    HPLOT V * I, 0 TO 0,N * (1 - I)
70    HCOLOR = 1
80    HPLOT V * I, N TO V * (1 - I), 0
90    HCOLOR = 3
100   HPLOT V,N * (1 - I) TO V * I, N
110 NEXT
120 END

 


 

Part 3  Procedures, functions and trigonometry

 

Lesson 27  Procedures, i.e., GOSUB and RETURN

GOSUB and RETURN are used to call and return from subprograms. A subprogram is a piece of code that we can call from different places in our program. Here’s an example:

10 PRINT "START OF PROGRAM"
20 GOSUB 100
30 PRINT "END OF PROGRAM"
40 END
100 PRINT "THIS IS THE SUBPROGRAM"
110 RETURN

In this program, GOSUB 100 tells the computer to go to line 100 and start executing code from there. When the computer encounters RETURN, it returns to the line that follows the GOSUB.


 

Lesson 28   Trigonometric functions

Trigonometric functions in Applesoft BASIC are basic mathematical tools that allow you to calculate the values of sine, cosine and tangent for given angles.

The list of these functions:

  • SIN(X) – returns the sine of angle X, where X is expressed in radians,
  • COS(Y) – returns the cosine of angle Y, where Y is expressed in radians,
  • TAN(Z) – returns the tangent of angle Z, where Z is expressed in radians,
  • ATN(L) – returns the arctangent (inverse tangent) of value L, giving the result in radians.

Below is the use of the SIN and COS functions in creating a rounded geometric construction. The number π has been defined as a simplified constant with the value 3.14.

10  REM RADIAL
20  HGR2 : PI = 3.14 : X = 20
30  A = 2 * PI / X : C = 1 / SIN (PI / X)
40  S = 140 : T = 95
50  FOR L = 1 TO X + 0.5
60    M = 0 : N = 0 : R = (L - 1) * A
70    FOR Q = 1 TO X
80      M = M + COS (R) / C
90      N = N + SIN (R) / C
100     U = 140 + INT (95 * M + 0.5)
110     V = 95 + INT (95 * N + 0.5)
120     HPLOT S,T TO U,V
130     S = U : T = V : R = R + A
140 NEXT : NEXT

This code is a bit harder to understand, but I include it in this basic course because it creates an interesting trigonometric pattern:

 

Lesson 29  Defining your own functions

We can also define our own functions using DEF FN. Here’s an example:

10 DEF FN SQ(X) = X*X
20 PRINT "THE SQUARE OF 5 IS "; FN SQ(5)
30 PRINT "THE SQUARE OF 2 IS "; FN SQ(2)
40 END

In this program we define a function called SQ that returns the square of a given number (the function multiplies two identical numbers together). Then we use this function to display the squares of numbers 5 and 2. We pass a parameter to the definition (in our case the digits 5 and 2); it is automatically assigned to the variable X that we defined in parentheses immediately after the name of the function.


 

Lesson 30  Graphics and text together

To finish, here’s another little program of mine that works simultaneously in graphic and text mode. It is a prime number benchmark – it checks prime numbers and graphically visualises their distribution:

It fills in a square for a prime number and leaves an empty square for each other number. The program takes about 20 minutes to fill the entire screen. At the beginning it runs quickly – it slows down as the value of prime numbers increases. I’ve left the benchmark in the English language version.

    10 REM PRIME-NUMBER BENCHMARK
    20 HOME:HGR
    30 COL = 2
    40 VTAB 24
    50 PRINT TAB ( 9); : INVERSE
    60 PRINT "PRIME-NUMBER BENCHMARK"
    70 NORMAL
    100 P = 0 : X = 0 : Y = 0
    110 FOR N = 2 TO 4000
    120 GOSUB 1000
    130 FOR K = 2 TO 2000
    140 M = N / K
    150 L = INT(M)
    160 IF L = 0 THEN 200
    170 IF L = 1 OR M > L THEN 190
    180 IF M = L THEN 250
    190 NEXT K
    200 VTAB 22
    210 PRINT TAB( 2);
    220 PRINT "PRIME-NUMBER: ";N;"  TOTAL PRIMES: ";B
    230 GOSUB 2000
    240 B = B + 1
    250 NEXT N
    1000 IF X > 294 THEN Y = Y + 5 : X = 0
    1010 HPLOT X,Y TO X + 5, Y
    1020 HPLOT X + 5, Y TO X + 5,Y + 5
    1030 HPLOT X + 5, Y + 5 TO X,Y + 5
    1040 HPLOT X,Y + 5 TO X,Y
    1050 P = 5 + 1 : X = X + 5
    1060 IF X = 275 THEN Y = Y + 5 : X = 0
    1065 IF Y = 155 THEN GOTO 2040
    1070 RETURN
    2000 FOR E = 1 TO 4
    2010 HPLOT X + 1, Y + E TO X + 4, Y + E
    2020 NEXT E
    2030 RETURN
    2040 PRINT TAB(20);: INPUT "PRESS ENTER "; Q$
    2050 END

Conclusion

If you understand how the above programs work, then congratulations — you are officially a programmer!

In this article I have used only part of the commands and functions of the BASIC language — you can find more in the Applesoft BASIC manual, which is available in English. You can download it here. An additional Applesoft manual is available here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.