Well here we are, the first instalment of my
OPL Tutorial series. What we will concentrate on in this section
is just to get a basic idea on how a program works, and how to
think like a programmer !!
Programming on the Psion in OPL is a handy way of doing things.
Reason being that you have the code with you all the time, and
secondly you are guaranteed that the program, once translated,
will work on all Psion Machines. So lets start shall we :-))
Firstly, let me explain the stages of writing a program using
your built in program editor. It is split up into 3 separate
stages:
Write your program in the program editor
Translate the program (This makes the code you've written,
readable by your Psion so it can be "run")
If there was an error whilst translating, the Editor will show
you where the problem in your code is, as well as give a
description of the type of error. Amend the problem, and then
re-translate
Happy?? Good :-)) Now we come to the terminology that is
associated with the Program Editor.
Start your OPL program editor (On the Series 3.x, you should find
this on the far left of your system screen. On the Series 5 this
is found in the "Extras" bar). Once the editor has
opened, you should be greeted with the following:
PROC :
ENDP
"What the hell are these things?", I hear you cry !!
Well let me explain - a program consists of many PROCedures,
which are called by a controlling PROCedure to perform certain
functions. In other words a PROCedure is a specific section of a
program that holds specific code, which is only called upon by
the main controlling section as and when it is needed.
So if you have been paying attention, you would have noticed that
the PROC and ENDP are keywords that mark the start and end of a
procedure respectively. To show this concept in a practical form,
I have written a small (Very small :-)) program just to
illustrate this concept:
PROC test:
PRINT "This is a very small program"
PAUSE 10
PRINT "Press a key to continue..."
GET
PRINT "Exiting..."
PAUSE 10
ENDP
In the above program, I have assigned a name to the procedure
(Test). You must remember this, as you call other procedures by
their assigned name. An explanation of the keywords in this
program are as follows:
PRINT - This keyword tells the program to
visibly display the words held in " "
PAUSE - This keyword tells the program to wait
for a specified time, as stipulated by the number after the
keyword
GET - Here the keyword GET, pauses the program
until a key is pressed
In future I will add REM (REMark) statements to the program. Do
not worry about this, as the Keyword REM tells the program to
ignore whatever is typed after it. This is a handy way of
remembering what certain parts of the program refer to, and
provides me with an easy way to explain to you what I am doing
:-))
Once you have typed in the above (Or copied and pasted),
translate it, and the OPL Editor will ask you whether you want to
Run the program. Press Y and the program will now run. This
program will display TEXT in the far left hand corner such as
follows:
This is a very small program Here the program pauses and then displays
the next text
Press a key to continue...
Here you will press any key to go
down to the next text
Exiting...
Here the program pauses again, and
then exits
And there you have it - you've
written your first program. Next, we are going to see how to call
a PROCedure.
Calling a Procedure
In programming, as was mentioned above, it is virtually
impossible to create a useful, lengthy program using only one
PROCedure. This is where a series of PROCedures is necessary to
perform various functions, without having a cluttered procedure
trying to perform everything. I will explain this using our above
example:
PROC test:
PRINT "This is a very small program"
PAUSE 10
PRINT "Press a key to continue..."
GET
key:
REM Here we are calling PROCedure "key"
ENDP
PROC key:
PRINT "Exiting..."
PAUSE 10
ENDP
Shown above is a simple example of how to call another procedure.
Our program went through the lines of code contained within
PROCedure "test" and performed all of the functions as
in the previous example, but instead of PROC "test"
performing all of the functions, it CALLED PROC "key"
to handle the Exiting part of the program. The end result is
exactly the same as above, but now we have a better structured
and neater program to work from.
Variables
Now that you are happy that you understand the basics behind how
to begin a program, we are going to go into a bit more detail.
Firstly we come to "Variables". Variables are used to
store values which may change.(eg: variable y may start with the
value 4, but later take the value 8. Please remember that
variables are a fundamental concept in programming, and you will
not be able to write any sort of useful program without them.
Variables must be DECLARED at the start of you procedure. For eg:
PROC test:
LOCAL x,y,z
REM The word LOCAL, tells the Psion to create variables with the
names that follow
REM ie: x, y, z - these are separated by commas
ENDP
The statement LOCAL x,y,z defines three variables, of which the
Psion will recognise whenever you use them in this procedure, NOT
other procedures. If you want variables to be recognised
throughout your program you must use GLOBAL instead of LOCAL, but
more on that later.
Before you go and start declaring variables left right and
centre, there are 4 types of variables you can use. The first 3
shown below relate to numbers, whereas the last one refers to
text:
Integer variable - this is used for small whole numbers eg: 6.
The range that this variable can hold is from -32768 to +32768.
If your program tries to add a number out of this range to an
integer variable, an error message will be displayed. Integer
variables have a % symbol on the end - eg: y%
Long Integer Variable - this used for numbers in the range of
-2147483648 to +2147483647. Long integer variables have a &
sign on the end - eg: y&
Floating point variables - this kind of variable is used for non
whole numbers ie: Decimal number such as 3.6332. If at any stage
you think that you program might generate a number that falls
outside the long integer variable range, or will turn into a
decimal number, then use a floating variable in this case.
Floating point variables do not end with anything - eg: y
String variable - this is used for text. A string variable ends
with a $ symbol followed by the maximum length of the string you
want the variable to handle in brackets eg: If you want to store
names up to 20 characters long, then you declare as follows:
y$(15) A string variable is limited to 256
characters.
There is also another way of declaring variables, which doesn't
require lots of writing. The way to do this is via an Array
variable. For instance if you wanted to declare 4 Integer
variables you could do this by typing LOCAL a%,b%,c%.d%
but with an array variable, you can achieve the same thing by
doing the following LOCAL a%(4). This creates 4 integer
variables: a%(1), a%(2), a%(3), a%(4) However for this course, we
will concentrate on the normal method of declaring variables.
To create an easy to understand program, I advise you to choose
descriptive names for your variables. For instance if the integer
variable you want to create relates to dollars, declare the
variable as dollars%. This will make it easy to understand your
code at a later stage.
Now we shall look at a simple program using variables:
PROC test:
LOCAL a%, b&, c, d$(5)
REM Here we have declared all of our variables for this procedure
a%=20
REM For the next 4 lines, we are assigning values to the
variables
b&=40500
c=2.435
d$="Hello"
PRINT a%
REM Here we are displaying the variables
GET
PRINT b&
GET
PRINT c
GET
PRINT d$
GET
ENDP
And there we have it. We have successfully assigned values to our
declared variables, and have managed to do something with them
(ie: print them to the screen)
In the next tutorial we look at LOOPS and then we will be able to
start to write something useful.
Cheers
Gary