• Keine Ergebnisse gefunden

DATA {~onstant} literal

Im Dokument BASIC Language Reference vs (Seite 195-200)

[,

{~onstant}]

...

literal

The DATA statement provides the values to be assigned to the variables in a READ statement. The READ and DATA statements allow tables of constants to be stored within a program.

Each time a READ statement is executed in a program, the next sequential value(s) listed in the DATA statements is obtained and stored in the receivers listed in the READ statement. The values entered with the DATA statement must be in the order in which they are to be used, and separated by commas. If several DATA statements occur in a program, they are used in order of statement number. Numeric variables in READ statements must reference numeric values;

alphanumeric receivers must reference literals.

When the compiler option MINANS = YES is selected, all data items are stored as literals and need not be enclosed in quotation marks. In addition, leading and trailing blanks are ignored.

The RESTORE statement resets the current DATA statement pointer so that DATA statement values are reused (refer to RESTORE in this section).

Example:

100 FOR I=l TO 5 200 READ W 300 PRINT W,W**2 400 NEXT I

500 DATA 5, 8.26, 14.8, -687, 22 Output:

5 25

8.26 68.2276 14.8 219.04 -687 471969 22 484

In the above example, the five values listed in the DATA statement are sequentially used by the READ statement and printed.

DATE Function

General Format:

DATE

DATE returns a six-character string that gives the current date in the form YYMMDD. The DATE function takes no arguments.

Example:

100 A$

=

DATE

200 PRINT STR(A$,3,2);"/";STR(A$,5,2);"/";STR(A$,l,2) 300 PRINT STR(DATE,3.2);"/";STR(DATE,5,2);"/";STR(DATEl,2) Output:

09/15/82 09/15/82

DEF Statement

General Format:

DEF function-name[%](v) = numeric exp where:

function-name

=

Any sequence of up to 64 letters, digits, and underscores, provided that the first character is a letter, and the name is not a VS BASIC reserved word.

v = The dummy variable, a numeric scalar variable and is optional only if MINANS is set to YES.

If % is present, the function returns an integervalue.

The define statement, DEF, enables you to define a single-valued numeric function within the program. Once defined, this function can be used in expressions in any other part of the program. The function may provide one dummy variable whose value is supplied when the

function is referenced. Defined functions can reference other defined functions, but recursion is not allowed. That is, a function cannot refer to itself, nor can one function refer to another function that refers to the first. The following program illustrates how DEF is used.

The DEF statement is also discussed in Section 4.4.2.

NOTE

A function can be defined anywhere in a program, but if the first use of a function precedes its definition, the function name must begin with the characters FN. Otherwise, the BASIC compiler interprets the function call as an array name reference. This results in either an error message at compilation time or in logic errors in the program.

Example:

100

x =

3

200 DEF OBFUSCATION(Z)

=

Z

**

2-Z 300 PRINT X + OBFUSCATION(2

*

X) 400 END

Output:

33

Processing of OBFUSCATION(2

*

X) in this example proceeds in the following order:

1. The expression specified as the argument of the function

OBFUSCATION (in this case, 2

*

X) is evaluated. Here, the value of the argument is (2* X

=

6).

2. The dununy variable in the function definition (in this case, Z in line 200) is temporarily assigned the value of the argument (in this case, 6).

3. The expression to the right of the equal sign in the function definition (line 200) is evaluated given the assignment just

performed, and the value is returned to the statement that invoked the function. In this case, (6 T 2 - 6)

=

30 is returned to the PRINT statement (line 400), which adds the value of X (3, in this case), and prints the result (33).

You can invoke a user-defined function from anywhere in a program.

The following restrictions apply to definitions of functions:

1. A DEF function cannot refer to itself. For example, DEF APPLE(MY_EYE)

=

MY EYE + APPLE(MY_EYE)

is illegal.

2. Two DEF functions cannot refer to each other. For example, the following combination of statements is illegal:

DEF ARTICHOKE(X) = BANANA(X) DEF BANANA(X)

=

ARTICHOKE(X)

Neither of the above restrictions is checked for during compilation, but both cause endless loops resulting in "stack overflow" during execution.

.~

The dummy scalar variable in the DEF statement can have a name identical to that of a variable used elsewhere in the program or in other DEF statements. Current values of the variables are not affected during function evaluation. DEF statements can also use other variables, using their current values at calling time.

Syntax Examples:

600 DEF JAGUAR(C)

=

(3

*

A) - 8

*

C + LION(2 - A) 700 DEF LION(A)

=

(3

*

A) - 9 I C

800 DEF TIGER(C)

=

LION(C)

*

JAGUAR(2)

DEF FN' Statement

General Format:

DEF FN' . t {(received.receiver) ... ) }

in literal[; literal] ...

where:

. {1 to 32 for Program Function key entries}

mt= Oto 255 for internal program references

The DEF FN' statement has two purposes:

1. To define a literal to be supplied when a Program Function (PF) key is used for keyboard text entry.

2. To define a PF key or program entry points for subroutines with argwnent passing capability.

Keyboard Text Entry Definition

To be used for keyboard entry, the integer in the DEF FN' statement must be from 1 to 32, representing the number of a PF key. When the corresponding PF key is pressed while execution is halted by an INPUT or STOP statement, your literal(s} is displayed and becomes part of the currently entered text line.

Each literal can be represented by a character string in quotes, a HEX function, or a combination of those elements.

NOTE

The PF keys can be defined to produce characters that do not appear on the keyboard by using HEX literals to specify the codes for these characters.

Im Dokument BASIC Language Reference vs (Seite 195-200)