• Aucun résultat trouvé

Using the BACKSPACE Statement-Keyed Access

Dans le document FORMAT (3(1) STOP (Page 138-141)

You can use one or more BACKSPACE statements to reestablish the position of a keyed file to a point prior to the current file position. You can then use a

sequential retrieval statement to read the record to which the file was just positioned.

If the key of reference has unique key values, the first BACKSPACE statement following a READ, WRITE, or REWRITE statement positions the file to the beginning of the same record that was just read or written. A BACKSPACE statement following a DELETE statement positions the file to the beginning of the record with the next lower key value. Subsequent BACKSPACE statements position the file to the beginning of the records with successively lower key values.

If the key of reference has nonunique key values, the first BACKSPACE statement following a READ, WRITE, or REWRITE statement positions the file to the first record with the same key value that appeared in the record that was just read or written. A BACKSPACE statement following a DELETE statement that deleted a record which was not the first record with that same key value, also positions the file to the first record with that key value. However, if the DELETE statement deleted the first record with a given key value, then the BACKSPACE statement determines the next lower key value and positions the file to the first record with that lower key value. Each subsequent BACKSPACE statement finds successively lower key values and positions the file to the beginning of the first record with those different key values. Therefore, when the key of reference has nonunique key values, a series of BACKSPACE statements does not position the file to all of the records that would be read with a series of sequential retrieval statements.

For example, if a sequence of records in file 8 were:

Record Key 2 Value

n D47

n+l D47

n+2 FlO

n+3 FlO

n+4 FlO

Assume you have just read record n+4 with key 2 as the key of reference. Then, two consecutive backspaces would position the file as follows:

Record Key 2 Value

n + 2 FlO (first backspace) n D47 (second backspace)

Because key 2 is nonunique, backspacing causes movement through a group of records to the first record of the group having a specific nonunique key value, and not to the next previous record, as it would if the key were unique.

Chapter 5. Programming Input and Output

111

You may use BACKSPACE to locate the last record, that is, the record with the highest key value in the file. First, you must position the file beyond the last record. You can do this in one of two ways:

• By issuing a sequential retrieval statement with the END=sln parameter after having already read the last record in the file. Control will pass to sIn in this case.

• By issuing a direct retrieval statement with a KEYGE or KEYGT parameter which specifies a search argument so large that no record in the file satisfies the search criterion. Control passes to the statement indicated by the NOTFOUND=sln parameter in this case.

A BACKSPACE statement issued when the file is positioned beyond the last record repositions the file to the beginning of the record with the highest key value.

(If there is more than one record with this key value, the file is positioned to the first such record.) A sequential retrieval may then be used to read the record with the highest key value.

Issuing the BACKSPACE statement has no effect if the file is positioned at the beginning of the first record in the file (such as after an OPEN or REWIND). It is not permitted if the previous retrieval or update operation failed for any reason other than reaching the end of the file.

~ _ _ ....-_ _ _ _ _ _ _ End of mM Extension _ _ _ _ _ _ _ _ _ _ -'

Chapter 6. Subprograms and Shared Data

You may need to write programs that require a specific operation to be performed again and again, with different data for each repetition; for example, a complex mathematical operation.

You can simplify the writing of such programs if you write the statements that perform a repetitiv~_operation as a separate subprogram. You can then simply refer to the subprogram throughout the program at the points where you need the operation done.

The program that requires the services of the subprogram is the calling program.

The subprogram itself is the called program.

Two kinds of subprograms that VS FORTRAN supplies are:

• Subroutine Subprograms--which are invoked in the calling program through the CALL statement. For example:

CALL CROOT (RNBR)

This CALL statement makes the value in RNBR available to subprogram CROOT and transfers control to the first executable statement in subprogram CROOT. When CROOT has completed execution, control is transferred back to the calling program.

• Function Subprograms--which are invoked in the calling program through function references. For example:

ANS

=

LNGTH

*

CROOT1 (RNBR)

When this statement is executed, the main program makes the value in RNBR available to function subprogram CROOT! and transfers control to the first executable statement in it. As soon as CROOT! finishes processing, control is returned to the main program together with a value that is multiplied by LNGTH to give ANS.

Calling and called programs can share data by means of common data areas and passed data items.

Chapter 6. Subprograms and Shared Data

113

Dans le document FORMAT (3(1) STOP (Page 138-141)