• Aucun résultat trouvé

PRINT J: J=J+1: GOTO

Dans le document the Programming (Page 33-36)

AlphabetiC Reference to BASIC Keywords

10 PRINT J: J=J+1: GOTO

Run this, then press STOP. CONT will cause execution to continue. You can change J, with J=10000, say, and CONT will resume with the new value.

cos

Type: Numeric function

Syntax: COS (numeric expression)

Modes: Direct and program modes are both valid.

Token: $BE (190)

Abbreviated entry: None

Purpose: Returns the cosine of the numeric expression, which is assumed to be an angle in radians.

Examples:

1. PRINT COS(45* 'If /180)

Prints the cosine of 45 degrees. Conversion from degrees to radians is per-formed by multiplying by 'If/180.

2. FOR J=O TO 1000 STEP 7f/10: PRINT COS(J):NEXT

Shows the cyclical nature of COS. Large values of the argument don't in-troduce significant error, because COS uses only the remainder in the range 0 to 2 7f.

DATA

Type: Data marker statement

Syntax: Data list of data constants separated by commas Modes: Program mode only

Token: $83 (131)

Abbreviated entry: D SHIFT-A

Purpose: Enables numeric or string data to be stored in a program. READ retrieves DATA in the same order it's stored in the program.

Notes:

1. DATA statements to store ML can be generated automatically (see Chapter 9).

2. ?SYNTAX ERROR, in a valid DATA statement, means READ and DATA don't match properly.

3. Unnoticed commas can introduce baffling bugs: DATA R,O,Y,G"B,P, contains eight data items, two of them null characters.

4. Because DATA statements are handled in sequence (RESTORE restarts the se-quence), be careful when adding data by appending a subroutine, in case data from a wrong routine is READ.

Examples:

1. 100 DATA "7975, LAZY RIVER ROAD"

Shows how double quotes enable commas, colons, and leading spaces to be included in strings.

2. 1000 DATA CU,COPPER,136.2, FE,IRON,35.1

Shows how sets of data can be stored. Typically, a loop with READ A$,M$,W might be used to READ each set of three items.

3. 10000 DATA SUB1

Might be used to insure that the correct data is being READ-use 1000 READX$: IF X$<> "SUBl" GOTO 1000 to locate SUB1.

DEF FN

Type: Statement

Syntax: DEF FN variable (variable)=arithmetic expression Modes: Program mode only

Token: DEF: $96 (ISO), FN: $A5 (165)

Abbreviated entry: DEF: D SHIFT-E (FN has no abbreviated form)

Purpose: Sets up a numeric (not string) function, with one dependent variable, which can be called by FN. Function definitions help save space where an expression

BASIC Reference Guide

needs to be evaluated often. However, their main advantage is improving BASICs readability.

Notes:

1. Direct mode is forbidden (but without good reason, since function definitions are stored along wth ordinary variables). See Chapter 6 on storage. Defined functions can be called by FN in direct mode.

2. UNDEF'D FUNCTION ERROR results if DEF FN hasn't been executed before FN is used. A ?SYNTAX ERROR caused by an invalid definition refers to the line us-ing FN, even when the line where FN is used is valid.

3. After loading a new program from BASIC, redefine any functions or they'll prob-ably not work. Chapter 6 explains why.

4. Function definitions work by calling a routine to evaluate expressions. Therefore, each definition must fit into one line of BASIC. The IF statement is not allowed, so logical expressions may be necessary. Calling another function definition is valid, however.

5. The dependent variable need not be used in the definition; if not, it is called a dummy variable.

Examples:

1. 100 DEF FN DEEK(X) = PEEK(X) + 256*PEEK(X + 1)

PRINT FN DEEK(50) prints the double byte stored in 50 and 51.

2. DEF FN MIN (X) = -(A>B)*B-(B>A)*A

Returns the smaller of A and B. Note the dummy variable X; any other vari-able could be used. The awkward form of the expression is necessary to fit it into a single statement.

3. DEF FN PV(I) = 100/(l + 1/100)

Sets up a present value function, where I is an annual interest rate.

4.1000 DEF FN E(X) = 1+ X+X*X/2+X*X*X/6+FN El(X) 1010 DEF FN El(X)=X*X*X*X/24 + X*X*X*X*X/120

Shows how a very long expression can be spread out.

DIM

Type: Statement

Syntax: DIM variable name [, variable name ... ] Modes: Direct and program modes are both valid.

Token: $86 (134)

Abbreviated entry: D SHIFT-I

Purpose: Sets up variable(s) in memory in the order they are listed in the DIM state-ment. This command is implicitly carried out when a variable is first used. Using a subscripted variable such as X(3) causes the equivalent of DIM X(10), so DIM is gen-erally used only when arrays which require more than ten elements are to be used, or when you wish to specify less than ten elements to conserve memory.

All variables and elements of arrays set up by DIM are set to zero (if numeric) or null (if strings).

Notes:

1. Arrays can use numeric expressions in their DIM statements, so their size can be determined by some input value. They don't have to be of fixed size. Arrays start with the zeroth element, so DIM X( 4) sets up a numeric array with five storage locations, X(O) through X(4). One-dimensional arrays can have a maximum of 32767 elements, and not more than 255 subscripts may be used in multi-dimensional arrays. In practice, ?OUT OF MEMORY ERRORs will result long before these limits.

2. Arrays are stored after variables. Chapter 6 explains the consequences of this.

Briefly, new variables, used after an array has been set up, cause a delay, and arrays can be deleted with POKE 49,PEEK(47): POKE 50,PEEK(48), so if inter-mediate results are computed with a large array, this array can be deleted when it is no longer needed.

3. RAM space occupied by arrays is explained in Chapter 6. Briefly, integer arrays are efficient, but string arrays are very dependent on the lengths of the strings.

PRINT FRE(O) gives a quick indication of spare RAM at any time.

4. Large string arrays are vulnerable to so-called garbage collection delays, also ex-plained in Chapter 6. The total number of separate strings, not their lengths, is the significant factor.

Examples:

1. 100 INPUT "NUMBER OF ITEMS";N: DIM IT$(N)

Might be used in a sorting program, where any number of items may be sorted.

Dans le document the Programming (Page 33-36)