ZX Format #06
29 июля 1997

Programmers - Beta Basic: continued talking about BASIC (Part 2).

<b>Programmers</b> - Beta Basic: continued talking about BASIC (Part 2).
          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. 

_______________________________





Other articles:

Today in the room - the contents of the magazine.

Authors - The authors of the journal ZX-Format No.6

From the authors - the long awaited event finally happened ...

Toys - The last iron (short story on the game 48 irons).

Toys - Many Adventures of Winnie the Pooh. Part Two.

Toys - the game description The Crypt (Castle Master 2).

Toys - description editor Adeventyur - PAW (Part 1).

Toys - description editor Adeventyur - PAW (Part 2).

Toys - description editor Adeventyur - PAW (Part 3).

Toys - description editor Adeventyur - PAW (part 4).

Toys - description editor Adeventyur - PAW (Part 5).

Programmers - Beta Basic: continued talking about BASIC (Part 2).

Programmers - General Sound: Programming Guide.

Programmers - MMD - the driver. Description of the structure of the modem driver for the terminal program MMD.

Programmers - AI on B. Mednonogova. A detailed description of the "wave of the algorithm" trace (automatic calculation of optimal) path, with an example implementation at Basic.

Programmers - Artificial Intelligence. Continuation of a series of articles about "AI". General basis for finding the way to goal.

Programmers - Tr-Dos for programmers. Max Petrov concludes his story about nontraditional methods of work with the disk.

Programmers - sharing experiences: "3-colour". Description of the effect of colors on 8-point ", help to the viewer, and how many words on the conversion of images in format "3-colour".

Programmers - sharing experiences: "3-colour". A few words about converting images in the format of RGB.

Programmers - the exchange of experience: programming Multicolor effects.

IS-DOS - users: how to personalize your system IS-DOS on a specific model of ZX Spectrum-compatible PC and to perform your tasks.

IS-DOS - users: how to copy the system disk IS-DOS and stay with the dead.

IS-DOS - the programmer: a short course - programming in IS-DOS.

IS-DOS - news: new software IS-DOS.

Iron - A short story about the capabilities of the processor Z-180.

Iron - Multiviewer. Description dorabotochki allowing to measure the speed of programs to curb without climbing in the codes - an easy push of a button.

Iron - A new project the firm Peters - "Sprinter". New Spectrum-compatible PC with a new generation of Speccy.

Iron - Opinions about skorpionovskom controller IDE HDD - SMUC.

Iron - SuperSpectrum: one project Spectrum-compatible machines. Its feature is compatible with the PC.

Iron - X-Trade FAQ. Answers to frequently asked questions on the GS and XTR-modem.

Premiere - Flash tracker. Description 4-channel editor of digital music, working with SoundDrive, from the author SoundDrive - Flash Inc.

Premiere - Description of the latest version of the universal terminal program used in SpbZxNet.

Premiere - Mortal Kombat: what awaits you in the full version of the game and some comments to the demo version.

Premiere - XReversy: presentation of a new toy from the popular family of "Solve puzzle - see the picture."

Interview - An interview with one of the most famous spektrumistov - Andrew Larchenko.

It was you - The story "Absolute Power".

It was you - The story "The Road".

It was you - Lord of the teeth: a parody of a popular trilogy ...

Mail - Contact us: an e-mail Alex'a from Nizhny Tagil, exhibited in the last room at the Corner of lamer. "

Mail - Letters from readers: Andrei Yakovlev, Denis Tokarchuk, Alex Garkulim, Alexander Gordeev, Evgenii Shumilov Nitochkin Vadim, Michael Larkin.

Mail - free advertising and announcements.

Miscellaneous - Scarecrow.: Nemo talks about the place of the PC and Spectrum in the modern Russia.

Miscellaneous - Review of Nemo in the book on digital circuitry. For anyone who has ever ever been tempted to turn on the soldering iron and ...

Miscellaneous - Questionnaire: Results of our poll spektrumistov.

Miscellaneous - Competition. A brief account of our contests.

Miscellaneous - The problems of the software market: when zagnetsya Spectrum. All over whether to blame hackers?

Miscellaneous - Outlook software. A brief overview of the forthcoming software: Fast Tracker, Pro Sound Creator, Black Crow.

Miscellaneous - Outlook software. Adventyura From Beyond or outside. "

Miscellaneous - A Memoir of the Peter modem network for ZX Spectrum - SPbZXNet.

Amiga Club - Between Us, by users: a comparison of characteristics of the Amiga 1200 with the IBM PC.

Amiga Club - compare the performance of Amigo and PC. As far as Amiga relevant in today's games?


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
Games - Death of little people! Cheats Black Raven 2 for Kung.

В этот день...   3 May