Program Declarations |
5 |
![]() |
Declarations
This section describes the label, constant, type, variable, and define declarations. Procedure and function declarations are described in Chapter 6, "Built-In Procedures and Functions."
Label Declaration
The label declaration defines labels, which are used as the target of goto statements. Comments
In Pascal, you can use both identifiers and integers as labels. Using identifiers as labels makes your code easier to read. Example
The commands to compile and execute label.p
|
hostname% pc label.p hostname% a.out Enter value for i: 101 Value of i too large. |
Constant Declaration
The constant declaration defines constants, values that do not change during program execution.
This constant declaration defines six valid constants.
|
const x = 75; y = 85; month = 'November'; lie = false; result = (x + y) / 2.0; answer = succ(sqrt(5+4)); |
Type Declaration
The type declaration describes and names types used in variable, parameter, and function declarations. Example
Variable Declaration
The variable declaration declares variables. Scope
The scope of a variable is either private or public.
Variables in the var declaration section of a program default to public when you compile your program without the -xl option. When you compile your program with -xl, variables default to private.
This code declares both public and private variables.
|
public var total: single := 100.00; quantity: integer16 := 25; private var score: integer16 := 99; |
Attributes
The variable attributes determine how to allocate the variable and its scope. They include static, extern, and define. static
A static variable is a variable that is private in scope and which is allocated statically. A global variable declared as static is equivalent to a variable that has been declared private. Pascal generates a compile-time error if you attempt to declare a global variable as both static and public.
The commands to compile and execute static.p
|
hostname% pc static.p hostname% a.out Call number: 1 Call number: 2 Call number: 3 Call number: 4 |
extern
The extern attribute is used to declare a variable that is not allocated in the current module or program unit, but is a reference to a variable allocated in another unit. You cannot initialize extern variables. See the Pascal 4.2 User's Guide, which describes separately compiled programs and modules; it also contains examples of the extern attribute. define
The define attribute is used to declare a variable that is allocated in the current module and whose scope is public. define is especially useful for declaring variables with the -xl option, which makes global variables private by default. See the Pascal 4.2 User's Guide for an example of this attribute. Initialization
You can initialize real, integer, boolean, character, set, record, array, and pointer variables in the var declaration. You cannot initialize a local variable (a variable in the var declaration of a procedure or function) unless you declare it as static.
Define Declaration
The define declaration controls the allocation of variables. Comments
The value of identifier must correspond to either a variable or procedure or function identifier. If identifier corresponds to a variable, it must have a matching variable declaration with the extern attribute. The define declaration nullifies the meaning of extern: it allocates the variable in the current program or module unit. Example
See the chapter on separate compilation in the Pascal 4.2 User's Guide for examples of the define declaration.
Procedure and Function Headings
This section discusses the visibility, parameters, the type identifier, functions, and options for procedure and function headings. Visibility
You can declare a procedure or function at the outer block level as either public or private.
Top-level procedures and functions declared in a program default to public when you compile your program without the -xl option. When you compile your program with -xl, all top-level routines declared in the program become private.
Parameter List
Pascal supplies the parameter types in, out, in out, var, value, and univ. Parameters: in, out, and in out
The in, out, and in out parameters are extensions to the standard, which are used to specify the direction of parameter passing:
var Parameters
With standard conformance options (-s, -V0, -V1), var parameters are the same in standard Pascal and Pascal. By default, the Apollo-like var compatibility approach applies: actual and formal records and arrays should be of the same type; other types of var must be of the same length.
Value Parameters
Value parameters are the same in standard Pascal and Pascal. univ Parameters
The nonstandard univ parameter type is actually a modifier used before data types in formal parameter lists to turn off type checking for that parameter. You can use it with any type of parameter except conformant array, procedure, or function parameters.
procedure somename (var firstparam: univ integer); |
You could then call this procedure with a parameter of any type. You should always declare a univ parameter as either in, out, in out, or var.
type real_array = array[1..100] of real; procedure receive(size: integer; var theArray: univ real_array); var n: integer; begin for n:= 1 to size do . . . |
Type Identifier
In Pascal, a function may represent a structure, such as a set, array, or record. In standard Pascal, a function can only represent the simple types of value, ordinal, or real. Functions Returning Structured-Type Results
If a Pascal function returns the result of a structured type, for example, an array, a record, a string, or some combination of these, you can construct or update the result, component-by-component, using assignments of the form:F S1 ... SN := E
where:
F:= 'The answer: 12 miles'where F is the function. However, sometimes you may want to obtain the string result by modifying some of the characters of an existing string (variable or parameter). In the following example, you may want to substitute a string for the string XX.
In general, an identifier of a function f returning a string can be used in an assignment of the kind:
f[i]:=c
for specifying the i'th byte of the function result. This Pascal extension can be used both for strings and varying strings. Example 2: A Function that Returns Arrays of Records
(Complex Vector Addition)
Options
Pascal supplies the standard forward routine option and the nonstandard options, extern, external, internal, variable, and nonpascal. forward
The forward option is the same in Pascal and standard Pascal. extern and external
The extern and external options indicate that the procedure or function is defined in a separate program or module. extern and external allow the optional specification of the source language of the procedure or function. For more information on these options, see the chapter on separate compilation in the Pascal 4.2 User's Guide. internal
The internal option makes the procedure or function local to that module. Specifying the internal option is the same as declaring the procedure or function as private. Pascal generates an error message if you attempt to declare a public procedure or function as internal. variable
Using the variable option, you can pass a procedure or function a smaller number of actual arguments than the number of formal arguments defined in the routine. The actual arguments must match the formal parameters types. You cannot pass a larger number of actual arguments than formal arguments. Example
nonpascal
Pascal supports nonpascal as a routine option when you compile your program with the -xl option. nonpascal declares non-Pascal routines when you are porting Apollo DOMAIN programs written in DOMAIN Pascal, FORTRAN, or C.