Beta-Basic
Part II
music by DNK
(C) D. Rudovsky
_______________________________
Procedures and functions.
For those working in BASIC, already
familiar with the concept of function, although the notion of
procedure and is probably
more abstract. So I'll start
description is to Procedures.
Spectrum-beysika procedure has been simplified to the routine
that does not have to procedures virtually irrelevant.
A distinctive feature of the procedure
is the presence of the name by which the
it is a challenge. Now I will give two
program illustrating what I said
above, the first of which was written under the
Spectrum BASIC, and the second under the BB:
I:
10 LET PRIVET = 100: LET I = 0
20 LET N = 3: GO SUB PRIVET
30 PRINT "I ="; I, "N ="; N
40 STOP ""
100 FOR I = 1 TO N
110 PRINT "HELLO!"
120 NEXT I
130 RETURN
Result:
HELLO!
HELLO!
HELLO!
I = 4 N = 3
II:
10 LET I = 7: LET N = 18
20 PRIVET 3
30 PRINT "I ="; I, "N ="; N
40 STOP
100 DEF PROC PRIVET N
110 LOCAL I
120 FOR I = 1 TO N
130 PRINT "HELLO!"
140 NEXT I
150 END PROC
Result:
HELLO!
HELLO!
HELLO!
I = 7 N = 18
Looking at the results you start to feel a strong sense of
surprise: After all I, as the loop variable must have a value
of 1 is greater than N, and even sign "HELLO!" was supposed to
appear at least 18 times, not 3. The truth is a bit confusing
lines 100 and 110, and even then some delirium in 20 line
program written in BB. Try to understand, by comparing these
two examples.
So, in 10 lines SB (Spectrum BASIC) PRIVET variable is assigned
a value of 100, ie, line number of routines, as well as the
variable I is 0. In this same line BB variables I and N are
assigned values 7 and 18 ootvetstvenno.
At 20 bar Sat a variable N is set to 3 and is called a
subroutine, which is in line addressable variable PRIVET. 20
lines BB represents for us a very great interest, since as at
first glance there is nothing in operator. In fact it is not
quite so because it is simply omitted, the operator PROC. But
then you ask: How is it omitted? Yes, in this sense, Sat more
conservative and he does not allow such things. BB is more
flexible, so the concept of "default" will meet us again and
again. For further explanation we have to go directly to the
100 line. Thus, 100 line BB:
100 DEF PROC PRIVET N
By analogy with the DEF FN can say that
, DEF PROC is a procedure definition, then PRIVET can be
regarded as its name, and N as a formal parameter. Formally, it
called because it functions as a
outside the procedure does not exist and therefore,
returning to the 20 line BB, we see that
there is a procedure call PRIVET with parameter 3.
The next line in the BB also of some interest, since it catches
the team LOCAL and in connection with the need to remember yet
another feature of procedures for which, in fact, was started
this whole fuss is with examples. The procedure is an
independent unit of the program due to the fact that it has a
so-called local variables, ie variables are valid only within
the procedure. The names of local and global (shared) variables
may be the same, but changes to local variables could
not reflected in the changing global variables. Thus, you do
not have all time to remember what variables you've used in the
program. Returning to the example we can say
That the command END PROC almost equivalent to the command
RETURN, but this does not mean that can arbitrarily change
their places as in the BB concept procedure differs
the notion of routine.
Now, explaining the concept of procedures,
proceed to use them, ie now
I try to tell, as soon as possible
and using explosives in the
this area.
Let's start first, ie with the parameters.
Rules for entering the parameters in a procedure similar to the
rules input parameters to functions in the Security Council,
except in those cases when the input parameters noted otherwise
Special teams of explosives. The first of them:
. REF a, or passing parameters by reference
where a - a variable name. This method is as
to rename the global variable
at the time of the procedure and allows to receive the result
of the procedure in a global variable. Here is an example of
ZX-REVIEW N1 for 1992:
10 LET X $ = "H1":
LET Y $ = "GOODBYE"
20 SWOP X $, Y $
30 PRINT X $, Y $
100 DEF PROC SWOP REF A $,
REF B $
110 LOCAL T $
120 LET T $ = A $: LET A $ = B $:
LET B $ = T $
130 END PROC
Result:
GOODBYE H1
Link is also used to pass as a parameter array, as
Arrays as parameters
directly prohibited.
Another team to simplify work procedures, as follows:
/ DEFAULT a =... Where a - a formal setting. This command
allows you to omit a parameter in the procedure call, ie allows
you to set the value of any option "default". As possibly
understand it can be quite difficult, then again, I will turn
to some examples:
10 MULT 10,5
1920 MULT 1910
30 MULT, 5
50 STOP
100 DEF PROC A, B
110 DEFAULT B = A: DEFAULT A = B
120 PRINT A * B
130 END PROC
Result:
50
100
25
In this case, when no one
parameter it is assigned by default
the value of another parameter.
Come and finish the story about the parameters
can the concept of the list. To indicate
what parameters are passed to a list of the keyword DATA.
What is the list of options and why you need it? There are
situations in which We do not know in advance the number of
parameters, in this case and apply the list parameters. Use the
parameters of the this list is not simple, but very simple.
But in the beginning, as always an example:
1910 SUMMA 1,2,3
1920 SUMMA 3,4,5,6,9
1930 SUMMA 1
40 STOP
100 DEF PROC SUMMA DATA
110 S = 0
120 IF ITEM () = 0 THEN GO TO 160
130 READ A
140 S = S + A
150 GO TO 120
160 PRINT "SUMMA ="; S
170 END PROC
Result:
SUMMA = 6
SUMMA = 27
SUMMA = 1
Here we used the function ITEM (),
which determines the next element
list, ie ITEM () = 0, if the list is exhausted, the pan; ITEM
() = 1 if the following parameters - -Number; ITEM () = 2 if
the following option - line.
DATA operator in this case was used to extract the parameters
of the list.
In using the features in the BB is no
significant changes compared to Sat
has not occurred, although the number of embedded
functions has increased significantly.
Conditional operators.
In the Council there is only one design
, The conditional operator IF ... THEN, HE In this
, The design has been expanded by the operator ELSE:
IF condition THEN K1 ELSE K2
Where K1 and K2, a group of operators. If the condition is
satisfied K1, otherwise the operator K2
GO TO ON a, n1, n2, n3, ...
GO SUB ON a, n1, n2, n3, ...
used to select the row number of transition that is a = 1
transition to the string n1; a = 2 in a row n2, etc.
The second form of this operator:
. ON a, k1, k2, k3, ... used to perform the operators from the
list. After executing the control is transferred the next line.
In addition to these forms is still
a specific form:
ON ERROR n
ON ERROR: k1: k2: ...
This command allows to intercept
Almost all of the error message, except:
0: OK.
9: STOP statement.
When using the first form at
mistakenly jumps to the line n.
In the second case we have a sequence of operators, k1, k2, ...
Disables the command:
ON ERROR 0
When handling errors can be useful are three special variables:
LINO - the line number in which
There was an error.
STAT - operator number in this
line.
ERROR - an error code.
After the execution of the statement ON ERROR
intercept errors off, so if
You want to constantly monitor the error, do not forget to
insert the recovery mode after the error handling.
Cycles.
Before you start to talk about
the use of cycles in the BB I have to
to sit back and talk about the types of cycles.
Those who know what the cycles with the precondition and
postcondition can safely skip the next few paragraphs. In the
series with the precondition is checked at the beginning of the
condition and, depending on its perform the loop body or holds
or going out of the loop. On Sat
loop with precondition can be
as follows:
10 LET I = 1: LET N = 10
20 IF I> = N THEN GO TO 50
30 PRINT I: LET I = I +1
40 GO TO 20
50 PRINT I: STOP
Result:
1
2
3
4
5
6
7
8
9
10
In the case when the condition is not satisfied immediately,
the loop will not also be satisfied. Loop with postcondition
always be carrying out at least once.
10 LET I = 1: LET N = 10
20 PRINT I: LET I = I +1
30 IF I <N THEN GO TO 20
40 PRINT I: STOP
Result:
1
2
3
4
5
6
7
8
9
10
As you can see the results of the work of the
and other types of cycles are the same, however,
if the condition in the loop with postcondition does not
will be executed immediately, the body all
still be done at least once.
In BB introduced a new design cycle:
DO
...
LOOP
Upon meeting the team is looking for explosives LOOP
Prior to her DO and passes control to the statement following
the DO. When used, use only the operators DO and LOOP we obtain
the so-called infinite cycle. Therefore introduced additional
command:
WHILE condition
UNTIL condition
These operators are placed after the operators
. DO and LOOP, defining the entry conditions of
cycle. If they are after the DO, it is
loop with a precondition, if after LOOP, then
this cycle with the postcondition.
WHILE indicates that the cycle will
performed as long as the condition is satisfied, and UNTIL
indicates that the cycle executed until the condition is not
satisfied.
Example:
I.
10 LET I = 1: LET N = 10
20 DO
30 PRINT I: LET I = I +1
40 LOOP
50 STOP
II.
10 LET I = 1: LET N = 10
20 DO WHILE I <N
30 PRINT I: LET I = I +1
40 LOOP
50 STOP
III.
10 LET I = 1: LET N = 10
20 DO UNTIL I> = N
30 PRINT I: LET I = I +1
40 LOOP
50 STOP
IV.
10 LET I = 1: LET N = 10
20 DO
30 PRINT I: LET I = I +1
1940 LOOP WHILE I <N
50 STOP
V.
10 LET I = 1: LET N = 10
20 DO
30 PRINT I: LET I = I +1
40 LOOP UNTIL I> = N
50 STOP
Unlike loop FOR ... NEXT here
prohibited from overlapping cycles.
For early exit from the cycle provides the operator:
EXIT IF condition
When the condition occurs in the transition operator followed
LOOP.
_______________________________