VMS Help PASCAL, Declaration Section, Routine Declaration *Conan The Librarian (sorry for the slow response - running on an old VAX) |
The basic algorithm for a program can usually be divided into relatively simple, repetitive tasks. In Pascal, you can code each task separately as a routine; that is, as either a procedure or a function. A procedure contains one or more statements to be executed once the procedure is called. A function contains one or more statements to be executed once the function is called; in addition, functions return a single value. A routine call executes all statements in the body of the declared routine. You must declare a routine before you can call it. In addition, function calls return a single value. Syntactically, procedure calls are statements, and function calls are expressions. You can call routines in the executable section of a program or in the body of another routine. Syntax: [[attribute-list]] PROCEDURE routine-identifier [[formal-parameter-list]]; { [[declaration-section]] BEGIN {statement};... END | {EXTERN EXTERNAL FORTRAN FORWARD} } Syntax: [[attribute-list]] FUNCTION routine-identifier [[formal-parameter-list]] : [[attribute-list]] result-type-id; { [[declaration-section]] BEGIN {statement};... END | {EXTERN EXTERNAL FORTRAN FORWARD} } The 'attribute-list' is one or more optional identifiers that provide additional information about the type-denoter. The 'routine-identifier' is the name of the routine. If you use the routine-identifier within the routine body (with the exception of assigning a value to the routine-identifier of a function), the result is a recursive call to the routine. The routine-identifier of a procedure can be redeclared in the procedure's declaration-section. The routine-identifier of a function cannot be redeclared in the function's declaration-section; however, it can be redeclared in any nested routines within the function's declaration-section. The 'formal-parameter-list' is a comma list of the routine's formal parameters. A procedure can have as many as 255 formal parameters; depending on the function return value, some functions are limited to 254 formal parameters. Optionally, you can specify a mechanism specifier and an attribute list for each parameter. The 'declaration-section' can include all sections except TO BEGIN DO, TO END DO, and VALUE sections. Data specified in this declaration section is local to the routine and to any nested routines; you can redeclare identifiers that are declared in an outer block. You cannot redeclare a formal parameter identifier to be a local variable in the routine. The 'statement' is any Compaq Pascal statement. In a function executable section, there must be at least one statement of the following form: routine-identifier := result The 'routine-identifier' is the name of the function. The 'result' is a value of either an ordinal, real, structured, or pointer type that VAX Pascal returns when function is called. This value must be of the same type as the result-type-id, and cannot be a file type or a structured type with a file component. 'EXTERN', 'EXTERNAL', 'FORTRAN', and 'FORWARD' are predeclared identifiers that direct Compaq Pascal to find the body of the routine elsewhere. The EXTERN, EXTERNAL, and FORTRAN identifiers declare routines that are independently compiled by Compaq Pascal or that are written in other languages. In Compaq Pascal, these identifiers are equivalent. Although not part of the Pascal standard, many Pascal compilers only accept the FORTRAN identifier for external routines actually written in FORTRAN; if portability is a concern, you may wish to use FORTRAN only for external FORTRAN routines. The 'result-type-id' is the type specification of the function return value. The function's result must be of this data type. This type cannot be a file type or a structured type with a file component. Example: PROCEDURE Read_Write( VAR A : INTEGER ); This declares a procedure, 'Read_Write', which takes one variable parameter, 'A'. Example: FUNCTION Counter( VAR Instring, Outstring : VARYING[10] OF CHAR; VAR Valid : Boolean ) : INTEGER; This declares a function, 'Counter', which takes three variable parameters, 'Instring', 'Outstring', and 'Valid' and returns an INTEGER value.
1 - formal_parameter_list |
A formal parameter is located in the header of the routine declaration, and consists of input parameters, output parameters, and routine parameters. A routine uses input parameters to obtain values; it uses output parameters to return values; and it uses routine parameters to call another routine named by a formal parameter. The formal parameter establishes the semantics, the data type, and the required passing mechanism of the parameter. See the "Compaq Pascal Language Reference Manual" for more information on parameter passing mechanisms. Syntax: [[({ {value-parameter-spec | variable-parameter-spec | routine-parameter-spec | foreign parameter-spec} };...)]] The specific format depends on the semantics (value, variable, routine, or foreign) of the formal parameter you are declaring. A formal value parameter represents a local variable within the called routine. When you specify value semantics, the address of the actual parameter is passed to the called routine, which then copies the value from the specified address to its own local storage. The routine then uses this copy. The copy is not retained when control returns to the calling block. Therefore, if the called routine assigns a new value to the formal parameter, the change is not reflected in the value of the actual parameter. Syntax: {identifier},... : [[attribute-list]] {type-id | conformant-parameter-syntax | undiscriminated-schema-name} [[:= [[mechanism-specifier]] default-value]] A formal variable parameter represents another name for a variable in the calling block. It is preceded by the reserved word VAR. When you specify variable semantics, the address of the actual parameter is passed to the called routine. In contrast to value semantics, the called routine directly accesses the actual parameter. Thus, the routine can assign a new value to the formal parameter during execution and the changed value is reflected immediately in the calling block (the value of the actual parameter changes). Syntax: VAR {identifier},... : [[attribute-list]] {type-id | conformant-parameter-syntax | undiscriminated-schema-name} [[:= [[mechanism-specifier]] default-value]] To write a routine that invokes another routine whose effect is not determined until the program is executed, use routine parameters. To declare a procedure or a function as a formal parameter to another routine, you must include a complete routine heading in the formal parameter list. You can also associate a foreign mechanism specifier and a default value with a formal procedure or function parameter. Syntax: [[attribute-list]] PROCEDURE procedure-id [[formal-parameter-list]] [[ := [[mechanism-specifier]] default-value ]] or [[attribute-list]] FUNCTION function-id [[formal-parameter-list]] : [[attribute-list]] result-type-id [[ := [[mechanism-specifier]] default-value ]] When declaring an external routine (one written in a language other than Pascal) that is called by a Compaq Pascal routine, you must specify not only the correct semantics but the correct mechanism as well. To allow you to obtain these passing mechanisms, Compaq Pascal provides foreign mechanism specifiers and the passing mechanism attributes. See the "Compaq Pascal Language Reference Manual" for complete details on formal parameter semantics.
1.1 - identifier
The 'identifier' is the name of the formal parameter. Multiple identifiers must be separated with commas.
1.2 - attribute_list
The 'attribute-list' is one or more optional identifiers which provide information about the formal parameter.
1.3 - mechanism_specifier
Compaq Pascal provides the foreign mechanism specifiers %IMMED, %REF, %DESCR, and %STDESCR, which precede a formal parameter in the declaration of an external routine. If the formal parameter does not represent a routine, the mechanism specifier must precede the parameter name. If the formal parameter represents a routine, the specifier must precede the reserved word PROCEDURE or FUNCTION in the parameter declaration. In addition, it is possible to use the passing mechanism attributes [IMMEDIATE] and [REFERENCE] in a formal parameter's attribute list to obtain the same behavior as %IMMED or %REF, respectively. A %REF or [REFERENCE] formal parameter requires actual parameters to be passed by reference. %REF or [REFERENCE] implies variable semantics unless the actual parameter is an expression; in that case, it implies foreign value semantics. An %IMMED or [IMMEDIATE] formal parameter requires actual parameters to be passed with the by immediate value mechanism and always implies value semantics. %IMMED or [IMMEDIATE] cannot be used on formal parameters of type VARYING, or on conformant array and conformant VARYING parameters. A %DESCR formal parameter requires actual parameters to be passed with the by descriptor mechanism and interprets the semantics as %REF or [REFERENCE] does. A %STDESCR formal parameter requires actual parameters to be passed with the by string descriptor mechanism. An actual parameter variable of type PACKED ARRAY OF CHAR implies variable semantics. An actual parameter expression of either type PACKED ARRAY OF CHAR or type VARYING OF CHAR implies foreign value semantics. You cannot use %STDESCR on formal procedure and function parameters.
1.4 - type_id
A type identifier is the type identifier of the parameters in this parameter section.
1.5 - undiscriminated_schema_name
The name of an undiscriminated schema type. If you have a user-defined, formal parameter of an undiscriminated schema type, the corresponding actual parameter must be discriminated from the same schema type as that of the formal parameter. When you pass a string expression to a formal, value parameter of type STRING, the actual parameter's current length (not its declared maximum length) becomes both the maximum length and the current length of the formal parameter.
1.6 - conformant_parameter
A conformant parameter is a syntax of a conformant array or a conformant VARYING parameter that represents a set of types that are identical except for their bounds. The bounds of a conformant parameter are determined each time a corresponding actual parameter is passed. The bounds of an actual parameter are available within the routine through identifiers declared in the schema. A conformant parameter can only appear within a formal parameter list. The form of a conformant array parameter is as follows: ARRAY [{lower-bound-id .. upper-bound-id : [[attribute-list]] index-type-id};...] OF [[attribute-list]] {type-id | conformant-parameter-syntax} PACKED ARRAY [lower-bound-id .. upper-bound-id : [[attribute-list]] index-type-id] OF [[attribute-list]] type-id The form of a conformant VARYING parameter is as follows: VARYING [upper-bound-id] OF [[attribute-list]] CHAR The 'lower-bound-id' is an identifier that represents the lower bound of the conformant array's index. The 'upper-bound-id' is an identifier that represents the upper bound of the conformant array's index. The 'attribute-list' is one or more optional identifiers that provide additional information about the conformant array. The 'index-type-id' is the type identifier of the index, which must denote an ordinal type. The 'type-id' is the type identifier of the array components, which can denote any type.
1.7 - default_value
Compaq Pascal allows you to supply default values for formal parameters. Using default parameter values, you do not need to pass actual parameters. Also, you can specify an actual parameter in the position of a formal parameter whose default value you want to override. This value can be any constant value of the type. It must be a legal actual parameter for the kind of formal parameter with which the default is associated. The default-value is evaluated when the routine is declared.
2 - block |
A block is a declaration section and an executable section. Programs, modules, and routines are structured in blocks. A declaration section can contain routine blocks nested within the outer program or module block; routine blocks can also be nested within other routines. Syntax: [[declaration-section]] BEGIN {statement};... END The declaration section contains data definitions and declarations, and nested routine declarations that are local to the enclosing block. The executable section contains the statements that specify the block's actions. You can exit from a block with the last executable statement of the block, which causes normal termination, or with a GOTO statement, which transfers control to an outer block.
3 - directive |
A directive is the alternative to a block in a routine declaration. A directive provides the compiler with information about either a routine whose heading is declared separately from its body (indicated by the FORWARD directive) or a routine that is external to the Pascal program (indicated by the EXTERNAL, EXTERN or FORTRAN directives). To specify a directive, include it immediately after the routine heading and follow it with a semicolon. The following describes the two classes of directives. o The FORWARD directive indicates a routine whose block is specified in a subsequent part of the same procedure and function section, allowing you to call a routine before you specify its routine body. As an extension, Compaq Pascal will allow the body to be in a different declaration part. If the body and heading are specified in different procedure and function sections, a FORWARD declared function should not be used as an actual discriminant to a schema type. When you specify the body of the routine in subsequent code, include only the FUNCTION or PROCEDURE predeclared identifier, the routine-identifier, and the body of the routine. Do not repeat the formal-parameter, the attribute-list, or the result-type-id. o The EXTERNAL, EXTERN and FORTRAN directives indicate that a routine is external to a Pascal program. They are used to declare independently compiled Pascal routines written in other languages. For portability reasons, the FORTRAN directive should only be used for external routines written in FORTRAN.
|