• Keine Ergebnisse gefunden

SCOPE OF IDENTIFIERS

Im Dokument Alpha PASCAL (Seite 68-74)

BEGIN { MAX }

5.5 SCOPE OF IDENTIFIERS

• •

ABS ARCCOS ARCCOSH ARCSIN

ARCSINH ARCTAN ARCTANH CH"RMOOE

CHR CLOSE CONCAT COPY

COS COSH CREATE CRT

DELETE EOF EOLN ERASE

ERROR ERRORINFO ERRORTRAP EXIT

EXP EXPONENT EXTENSION FACTORIAL

FILLCHAR FILESIZE FSPEC GET

GETFILE GETLOCKS IOSEARCH INCHARMOOE

INSERT JOBOEV JOBUSER KILCMO

LCS LENGTH LINEMOOE LN

LOCATION LOG LOOKUP MAINPROG

MARK MEMAVAIL MOVELEFT MOVERIGHT

NEW 000 OPEN OPENI

OPENO OPENR ORO PAGE

PF'ILE POS POWER PRED

PUT PVIRT PWROFTEN PWROFTWO

RA050 RANDOMIZE ROC ROI

ROR ROS READ REAOLN

RELEASE RENAME RESET REWRITE

RLN RND ROUND SCAN

Main Program

BlockA BlockS

BlockA1 BlockB1

BlockA2 BlockB2

Figure 5-1

Nested Structure of Prog~am Blocks

Let.'s say that a constant is defined both in the main program and BlockA.

BlockA itself and the blocks enclosed in BlockA (BlockA1 and BlockA2) use the definition made in BlockA. The main program, BlockB, BlockB1, a.nd BlockB2 use the constant definition made in the main program.

The following small program demonstrates identifier scoping. The variable Counter is declared both within the main program and within the procedure InnerBlock:

(Changed 30 April 1981)

PROGRAM Scope { This program tests identifier scoping };

VAR Counter INTEGER;

--{ "Counter" dec Lared for main program}

PROCEDURE InnerBLock;

VAR Counter: INTEGER;

{ "Counter" decLared for Procedure InnerBLock } BEGIN

Counter := 1;

FOR Counter := 1 TO 10 DO BEGIN

WRITELN('Procedure InnerBLock-- Counter = ',Counter);

END

END ~nd Procedure InnerBLock };

BEGIN { Main Program}

Counter := 20;

WRITELN('Main Program-- Counter = ',Counter);

InnerBLock { Invoke Procedure InnerBLock };

WRITELN('Main Program again-- Counter = ',Counter) END { End Main Program }.

If our description of identifier scoping is correct, we would expect the statement:

WRITELN('Main Program again-- Counter= ',Counter)

to produce the vaLue 20, regardLess of the vaLue assumed by Counter within the procedure InnerBLock. That is exactLy what happens.

5.6 NOTATION

ALphaPascaL uses severaL conventions in handLing and representing numbers and strings.

5.6.1 NUMBERS

The integer a fractionaL and which is zero).

PascaL recognizes two types of numbers: integer and reaL.

numbers are the "whoLe numbers"; that is, they cannot contain part. ReaL numbers are numbers that contain a decimal point, therefore contain a fractionaL part (even if that fractionaL part For exampLe, these numbers are integers:

-231 7 8098

These are real numbers:

567.8 -25.00

4.318

(For information on the REAL and INTEGER data types, see Chapter 7, "Data Types.") Pascal has two methods of displaying numbers: decimal notation and scientific notation. DecimaL notation aLLows us to represent a number with an optionaL sign, a whoLe number part, a decimaL point, and an optionaL fractionaL part. If the fractionaL part exists, there must be at Least one digit on each side of the decimal point. For exampLe:

-2405.3

Scientific notation is handy for representing very smaLL or very large numbers. A number represented in scientific notation is shown as a vaLue multipLied by the appropriate power of 10. To indicate the exponent, Pascal uses the symboL "E". For example:

-2.4053E+3

represents "negative 2.4053 times 10 to the thi rd"; that is, in decimaL notation, the number wouLd be -2405.3. A positive number after the E teLls you how many pLaces to shift the decimal point to the right, in order to read the number in decimal notation; a negative number teLLs you how many pLaces to shift the decimal point to the left. For exampLe, to represent the number:

5.678E-2

in decimal notation, shift the decimal point to the Left two pLaces: 0.0567.

AlphaPascal generaLLy uses decimaL notation to dispLay real and integer numbers. (Of course, if the number is integer, no fractionaL part is shown.) However, if a number is too large or too smaLL to represent easiLy in decimaL notation, AlphaPascaL dispLays it in scientific notation.

You may use either scientific or decimaL notation when entering numbers to a PascaL program, or within the program itseLf.

(For information on using the WRITE and WRITELN procedures to format numeric and character output, see Section 10.1.5.5, "Formatting Output.")

5.6.2 STRINGS

A string is a group of characters. These characters may be numbers, letters, or any combination of characters, incLuding the delimiters for a comment-- {} or (* *). A string is identified to PascaL by encLosing it in singLe quotation marks. For exampLe:

'This is a string. ' I Data: 1231

'The END is nearl

The characters in a string represent themseLves, rather than numeric vaLues, reserved words, etc. For exampLe, the third exampLe contains the characters

"123", but does not represent the number 123. The fourth exampLe contains the characters "END", but does not represent the keyword END.

If you wish a string to contain a quotation mark, pLace two quotation marks where you want the singLe quotation mark to appear. For exampLe:

Iyou don"t say.1

A string may be defined in a constant definition. For exampLe:

CONST Message

=

'Error - Type CR to recoverl

(We then say that Message is a string constant.) Or, a string may be used as a string LiteraL. For exampLe:

WRITELN('Do not forget to write-enabLe the disk.')

NOTE: ALphaPascaL incLudes the data type STRING as a standard data type.

Data of type STRING consists of a group of characters (data of type CHAR) rather than a singLe character. For information on CHAR and STRING, see Chapter 7, "Data Types."

DECLARATIONS AND DEFINITIONS

One of the important features of Pascal is that it requires that you define and name the data objects you are going to use in a program before you reference those objects. For example, if you are going to be using a variable named "Cost", you must "declare" that variable at the start of the program or procedure in which that variable appears. Besides declaring variables, you must also declare the program name, labels, functions and procedures, and modules. In addition, you must define any numeric or string constants you are going to use, as well as any data types. All declarations and definitions appear at the front of the main program or the procedure or function containing the declared data objects.

These centralized declarations and definitions legibility and organization of your program, performing error detection.

greatly and aid

enhance the the compiler in You'll remember from Chapter 5 that the declaration and definition part of the program block takes the form:

Label declarations Constant definitions Type declarations Variable declarations External declarations

Procedure and function definitions

Im Dokument Alpha PASCAL (Seite 68-74)