We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
2,862 Tutorials

How to program software: part II

We conclude our SmallBasic masterclass

Step 4. If you use a GOSUB statement instead of GOTO, the program will jump to the line number specified; a RETURN statement returns it to the line after the GOSUB. The code between the lines referred to in the GOSUB and the RETURN is called a subroutine. These make code easier to read and allow you to use it in different parts of your program.

SmallBasic step 4

Step 5. We’ll use data in a separate file to draw a pattern. Using Notepad, save a file called DATA.TXT in the same folder as your SmallBasic programs. Enter the following values:
250, 0
500, 200
375, 200
375, 500
125, 400
125, 200
0, 200
250, 0
-999, -999

SmallBasic step 5

Step 6. Alter the previous walkthrough's Step 11 program. Add OPEN “DATA.TXT” FOR INPUT AS #1 to the beginning. Change the two READ statements to INPUT statements. These usually prompt you for input data, but the following variant reads data from the file number specified in the OPEN statement: ‘INPUT #1, X, Y’.

SmallBasic step 6

Step 7. SmallBasic features many functions that work with numeric values. The program shown below uses SIN and COS, but for trigonometric functions the angle must be in radians rather than degrees; D2R is a conversion factor. Note the rounding errors in the results – sin(90) is really 0.

SmallBasic step 7

Step 8. Enter PSET SCREENX, SCREENY COLOR 1 inside a FOR/NEXT loop that increments SCREENX from 0 to 599. Put this inside a FOR/NEXT loop that increments SCREENY from 0 to 399. You’ll see a black block. Temporarily put ‘DELAY 10’ after the PSET to see that it draws one dot at a time. Save this program.

SmallBasic step 8

Step 9. Start a new program. INPUT the real and imaginary parts, call a squaring subroutine (starting at line 500), and PRINT the answer. The real part is real squared minus imaginary squared; the imaginary part is two times real times imaginary. Test with real -= 0.1, imaginary = 1; the result should be real = -0.99, imaginary = 0.2.

SmallBasic step 9

Step 10. Next, INPUT two complex numbers. Alter the subroutine to add the second complex number to the result of squaring the first. The real part of the sum of two complex numbers is the sum of the real parts. The imaginary part is the sum of the two imaginary parts. Verify the results with an online complex calculator.

SmallBasic step 10

Step 11. Modify the previous program to INPUT a complex number and PRINT a message as to whether it’s in the Mandelbrot Set. To get the modulus square the real and imaginary parts, add them together and square root the sum. You should find that real = -1, imaginary = 0 is in the set, and real = -2, imaginary = 1 is not.

SmallBasic step 11

Step 12. Use Step 11’s logic inside the nested loops of Step 8’s program to test points on the complex plane, drawing a dot with the PSET statement only for points in the Mandelbrot Set. Test points from real = -2 and complex = 1 to real = 1, complex = -1. Derive these from SCREENX and SCREENY (for example Real = SCREENX/200-2).

SmallBasic step 12

Step 13. The Mandelbrot Set is often shown in colour, with points outside the Set displayed as contours. Modify the previous program to plot points that aren’t in the Set, using a colour derived from the count of how many iterations it required to exceed a modulus of two. Colour = 16 – Count / 8 works well.

SmallBasic step 13

Step 14. The Mandelbrot Set is a fractal figure. The more you zoom, the more detail you see. To fully appreciate it, modify the software to show an area for which you specify the real and imaginary parts of the complex number and a zoom factor. The screenshot below shows what you’ll see if real = -1.15, imaginary = 0.4, and zoom = 5.5.

SmallBasic step 14

IDG UK Sites

Samsung Galaxy S6 review: Hands-on with the new Samsung Galaxy. Samsung's flagship is more iPhone-lr......

IDG UK Sites

5 things we hate about MWC: What it's like to be a journalist at a technology trade show

IDG UK Sites

Ractive powers The Guardian's interactive infographics – and lets novice coders build complex...

IDG UK Sites

What does that mean? A jargon-buster dictionary of tech terms for Apple fans