40 STOP
100 LOCAL a$="Different Value"
110 PRINT a$
120 RETURN
This will print Different Value and Test on your screen as LOCAL creates in a sense two versions of a$. The second one exists only until the RETURN keyword is reached.
PRIVATE is similar to LOCAL in the fact that's reserved only for subroutines (see above) and procedures (see next section). Its syntax is as follows:
PRIVATE var[=value]
where var is a numeric variable (Unlike LOCAL, only numeric variables are accepted by PRIVATE) and value is the optional initialisation value. The latter is important as you will see below. PRIVATE ensures that the variables that follow it, are both independent of the rest of the program and that they are retained every time the procedure is called. In order to reset the value of all private variables (to 0 or to the value initialised to by the PRIVATE statement) we can use PRIVATE CLEAR. When calleded from within banked code (See Chapter 23 – The Memory for information about banks and banked code) PRIVATE CLEAR initialises only the private variables for the procedures in that bank otherwise the private variables for the main program are reset. A small example on how PRIVATE and PRIVATE CLEAR work is the following:
100 PRIVATE CLEAR
110 FOR i=1 TO 10: PROC
iterate():NEXT i:STOP
120 DEFPROC iterate()
130 PRIVATE callcount=0
140 callcount+=1
150 PRINT "I have been called
";callcount;"times"
160 ENDPROC
Procedures are a special form of subroutines. Imagine them as a combination of subroutines and functions (See Chapter 8 – Functions). Like subroutines and the GO TO keyword they branch execution to a different segment of the program to better organise and reuse code. Unlike subroutines but like functions, they can accept multiple variables as parameters, can be named and when called they do not require a line number or label.
Think of procedures as a way to extend NextBASIC commands much like user defined functions extend the inbuilt functions (See Chapter 8 for more details).
Procedure parameters can be regular numeric, integer and string variables as well as arrays which follow all the naming conventions of the former (As seen in Chapters 1 – Basic Programming Concepts , 7 – Expressions and 11 – Arrays).
Procedures can carry meaningful names following the naming conventions of numeric variables (See Chapter 7 – Expressions for valid numeric variable names), while they can also optionally be followed by a $ sign. The latter has no effect in functionality but can be used to identify what the procedure does (for example manage strings)
Procedures are defined by the keywords DEFPROC which takes the form:
DEFPROC name[$] ([parameter1[=def1][,...[, parameterN[=defN]]]])
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.