|
The SiMPLE Tutorial (Part 2)
("The Fun and Easy Way to Become a Programmer")
|
In Part One of this tutorial, we gave you practice at typing-in and running a Micro-SiMPLE program that we wrote for you. Now you are about to discover the fun of being able to easily write your own Micro-SiMPLE computer programs!
Computer Programs
A computer program is a list of instructions, similar to a recipe. The computer performs (or executes) these instructions, starting at the top of the list, and works its way down toward the bottom, executing one instruction at a time.
However, unlike a recipe (which is generally written in a "human language, such as English), a computer program is written in a unique language designed especially for computers. The programming language we are going to be using is called "Micro-SiMPLE ", a subset of the entire SiMPLE language. (In this tutorial we will usually use the generic term "SiMPLE" instead of "Micro-SiMPLE" if the material being discussed is not limited to only Micro-SiMPLE.)
Programming your Computer
The process of creating a program can be divided into three major steps:
Step 1: Writing the Program
Step 2: Entering the Program into the Computer
Step 3: Running the Program
(In practice, the first two steps are often done simultaneously.)
You are already familiar with Steps 2 and 3 from your experiences in the first part of this tutorial. We will now focus on Step 1: Writing the Program.
The "Rules of the Game" for Programming
In this section we will briefly describe the basic rules that you will need to know in order to write programs in Micro-SiMPLE.
"In The Beginning..."
As you know, there are two different "flavors" of SiMPLE: Pro-SiMPLE and Ultra-SiMPLE. Each one exists in its own separate "universe". (Click here to read more.) Micro-SiMPLE exists in both universes but, depending on the application, some features work better in one universe than in the other. In this tutorial, we will be working only in the "Ultra-SiMPLE" universe. Therefore, every Micro-SiMPLE program listing in this tutorial will start with the word:
Ultra-SiMPLEso that the computer will know which universe we have chosen to use. (If you forget to specify a universe, the default* universe will be used.)
*(The default universe is the one that was last used when in Command-line mode.)
Statements and Keywords
The individual instructions which makes up a program are commonly referred to as statements. Each statement in Micro-SiMPLE begins with one of four special words, each word representing one of the four major concepts of programming in Micro-SiMPLE. These four special words (called keywords) are:
Call Set If Goto
We will discuss each of these keywords shortly.
Every keyword must begin with an upper-case letter. In fact, the only time you are allowed to use upper-case letters (other than with words contained inside a pair of quotation marks) is when specifying "system" words such as keywords, or the names of "tasks" in the System Library. (We will discuss tasks in the next section.) No other words should contain any upper-case letters.
Tasks
A task is an extremely important concept in computer programming. (In fact it may be the most important concept.) A task can be thought of as being similar to a little black box that automatically does "something" special. You do not need to know how the black box does what it does. All you need to know is what it accomplishes. (In some other programming languages, "tasks" are referred to as "subroutines".)
By using tasks, most of computer programming reduces to the relatively simple job of snapping together "black box modules" to form a complete program.
This "building block" concept of connecting modules together to create a program constitutes much of the basis of the SiMPLE language and philosophy. By using this concept, programs that would normally be difficult for a novice to write can now be created easily and quickly by using a "construction kit" approach to programming. Just as it is easy for almost anyone to build a small plastic model airplane from a model airplane kit, so too is it easy for almost anyone to "build" computer programs from task modules.
Some tasks are complete in and of themselves. Other tasks require the user to specify additional information as to precisely how the task is to perform its operation. This additional information is referred to as a calling parameter. (Tasks can have more than one calling parameter.) We will illustrate the use of calling parameters shortly.
Libraries
Every task exists in a library. There are three types of libraries in SiMPLE:
A - The System Library is the standard library of SiMPLE.
It contains tasks which perform very general and primitive
operations (such as clearing the screen, plotting a point,
drawing a line, etc.).
B - Import Libraries contain tasks produced by third-party
suppliers (such as a software store, a friend, etc.). Such tasks
generally perform more specialized functions (such as drawing
a rocket, creating an explosion, etc.). SiMPLE comes with
one pre-imported library already installed for you.
C - Append Libraries contain tasks that you've created.
(We'll show you how to create and use your own tasks
later in this tutorial.)
Keyword: Call
The keyword "Call" is used to invoke a task from any of the libraries.
Calling Tasks From The System Library
|
Every task in the SiMPLE System Library is invoked using the following general form:
Call task (parameters ) where:
Call - is a keyword which indicates that a task is being invoked. |
An Example
Let's start out by writing a program that invokes the System Library task named "Circle". The "Circle" task requires you to specify three calling parameters -- the X coordinate of the center, the Y coordinate of the center, and the radius. (We will discuss graphics coordinates later in this tutorial.)
The following program invokes the "Circle" task:
| Source Listing |
(Yes, those two lines constitute a complete program! Computer programs do not have to be long and/or complicated.)
Now that we've written our program, let's put it into our Project Folder and run it. (We'll let you choose a name for the program.)
If you didn't make any typing mistakes you should see a white circle on your display.
Congratulations! You've just written and executed your first SiMPLE computer program! (Now that wasn't so hard, was it?)
Go ahead and change the three calling parameters to slightly higher (or slightly lower) values and see how they effect the size and location of the circle on the screen. (Later on we will explain what these numbers mean. But for now, just go ahead and experiment with them.)
A Brief Note About Error Messages
Of course, you never make misteaks!
But, unlike you, there are a few programmers who, once in a while, write programs that don't always work
correctly when they try to run them. So (for their benefit
),
we will mention a few words about error messages that can occur when an incorrect program is written.
For example, suppose you had accidentally mistyped the second line of our previous program as:
Call circle (320, 240)
(without the "50" term). If you had tried to run that program, the computer would have beeped at you and displayed the following message on the screen:
line 2: Not enough parameters given Call circle (320, 240)
This error message would have occurred because the Circle task requires three parameters, and our program would have specified only two of them. (We would have forgotten to specify the radius of the circle.)
If a program has an error, SiMPLE does its best to report the error by telling you the line number in your program in which it thinks the error exists, followed by the line itself. Sometimes, however, the actual error may be located somewhere else in your program, and not at the line specified. But, in any case, you'll know that an error exist somewhere in your program.
Let's Change the Color of the Circle
To do so, all we have to do is call the "Linecolor" task before calling the "Circle" task:
| Source Listing |
When you run the program now, it will draw a green circle. The calling parameter "2" to the "Linecolor" task specifies that the color "green" is to be used whenever any lines are drawn. (Later on we will explain these "color numbers". But for now, just go ahead and see for yourself what happens when you try using different values.)
Another Example
As another example of a program that invokes tasks from the System Library, consider the listing shown below:
| Source Listing |
The "Honk", "Cuckoo", etc. tasks generate their respective sounds. The "Delay" task prevents the computer from executing the next statement for a specified number of milliseconds. (Go ahead and run the program.)
The "Output" Task
One of the most important tasks in the System Library is the "Output" task. It is used to display words and numbers on the screen. For example, the following very short program:
| Source Listing |
will display the words:
SiMPLE for kids!
on the screen. And this one:
| Source Listing |
will display the number 7 on the screen (because the "3+4" portion was not inside a pair of quotation marks).
(We will have more to say about the "Output" task later on.)
Calling Tasks From an Import Library
|
Every task in an Import Library is invoked using the following
Call task (parameters ) @ handle where:
|
Notice the similarity between calling tasks in the System Library and calling tasks in an Import Library. The only obvious difference is that when calling tasks in an Import Library, you must generally supply an additional piece of information called a "handle ".
Another difference is that tasks in an Import Library always have names that begin with a lower-case letter. Tasks in the System Library have names that can (at your choice) begin with either a lower-case or an upper-case letter when you invoke them.
Example
SiMPLE comes with only one pre-installed Import Library (the "toys" library). All of the tasks in that library require the handle "toys" to be specified when using them. Let's play with "rocket" and "explode", two of the tasks in the "toys" library.
The "explode" task produces a brief explosion at a specified location on the screen. The "rocket" task draws a rocket at a specified location on the screen.
Let's use these tasks from the "toys" library to create the following program:
| Source Listing |
If you run this program, you should see a rocket appear somewhere on the screen and, after three seconds, see it explode.
As these first several example programs have demonstrated, the ability to invoke library tasks makes creating computer programs extremely easy, especially for a beginning programmer. (All the "hard stuff" has been taken care of for you.) In fact, even if you were to stop right now and not finish reading the rest of this tutorial, you already have enough knowledge to start creating your own little "fun" programs. (But please do keep on reading!)
![]()
[ Contact Us | Homepage | Online Forums | About the Author ] © Copyright 2007 SiMPLE CodeWorks, Inc. All rights reserved. |