[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Pascal for OpenVMS
Language Reference Manual


Previous Contents Index

5.6 Empty Statement

The empty statement causes no other action to occur than the advancement of program flow to the next statement. To use the empty statement, place a semicolon where the language syntax calls for a statement.

Consider the following example:


CASE Alphabetic OF
   'A','E','I','O','U' : Alpha_Flag := Vowel;
                   'Y' : ;  {Empty statement as selector; no action}
   OTHERWISE Alpha_Flag := Consonant;
   END;

5.7 FOR Statement

The FOR statement is a looping statement that repeats execution of a statement according to the value of a control variable. The control variable assumes a value within a specified range or set. A FOR statement has one of the following forms:


                                               {TO      }
         FOR control-variable := initial-value {DOWNTO  } final-value DO statement

         FOR control-variable IN set-expression DO statement

control-variable

The name of a previously declared variable of an ordinal type.

initial-value

final-value

Expressions that form a range and whose type is assignment compatible with the type of the control variable.

set-expression

An expression resulting in a value of SET type. The base type of the set must be assignment compatible with the control variable.

statement

Any Pascal statement that does not change the value of the control variable.

At run time, the initial- and final-values or the set-expression is evaluated before the loop body is executed. Execution or termination of the statement occurs in the following cases:

  • In the TO form, Pascal checks to see if the value of the control variable is less than or equal to the final-value. If this condition is met, the control-variable takes on the value of the initial-value for the first loop iteration. During iterations, the control variable increments according to its data type. Looping ceases when the control-variable is greater than the final-value.
  • In the DOWNTO form, Pascal checks to see if the value of the control-variable is greater than or equal to the final-value. If this condition is met, the control variable takes on the value of the initial-value for the first loop iteration. During iterations, the control-variable decrements according to its data type. Looping ceases when the control-variable is less than the final-value.
  • In the set-expression form, Pascal checks to see if the set-expression is not the empty set. If this condition is met, the control-variable takes on the value of one of the members of the set. Iterations occur for each member of the set; the selection order of members of the set is undefined. Looping stops after the loop body executes for each member of the set.
In both the TO and the DOWNTO forms, how the control-variable increments or decrements depends on its type. For example, values expressed in type INTEGER increment or decrement in units of 1. Values expressed in type CHAR increment or decrement in accordance with the ASCII collating sequence.

After normal termination of the FOR statement, the control-variable does not retain a value. You must assign a new value to this variable before you use it elsewhere in the program. If the FOR loop terminates with a GOTO statement, the control-variable retains the last assigned value. In this case, you can use the variable again without assigning a new value.

Consider the following examples:


FOR Year := 1899 DOWNTO 1801 DO  {Print leap years in 1800's}
   IF ( Year MOD 4 ) = 0 THEN
      WRITELN( Year:4, ' is a leap year' );

FOR I IN Set1 DO  {Set2 members are successors of Set1 members}
    Set2 := Set2 + [I + 1];

For More Information:

5.8 GOTO Statement

The GOTO statement causes an unconditional branch to a statement prefixed by a label. A GOTO statement has the following form:


GOTO label

label

An unsigned decimal integer or symbolic name that represents a statement label.

The GOTO statement must be within the scope of the label declaration. A GOTO statement that is outside a structured statement cannot jump to a label within that structured statement. A GOTO statement within a routine can branch to a labeled statement in an enclosing block only if the labeled statement appears in the block's outermost level. Consider the following example:


FOR I := 1 TO 10 DO
   BEGIN
   IF Real_Array[I] = 0.0 THEN
      BEGIN
      Result := 0.0;
      GOTO 10;       {Use GOTO to exit from loop}
      END;
   Result := Result + 1.0/Real_Array[I];  {Compute sum of inverses}
   END;

10: Invertsum := Result;

For More Information:

5.9 IF Statement

The IF statement tests a Boolean expression and performs a specified action if the result of the test is TRUE. The ELSE clause, when it appears, executes only if the test condition results to FALSE. An IF statement has the following form:


IF boolean-expression THEN statement1 [[ELSE statement2]]

boolean-expression

Any Boolean expression.

statement1

The statement to be executed if the value of the Boolean expression is TRUE.

statement2

The statement to be executed if the value of the Boolean expression is FALSE.

If an IF statement contains an ELSE clause, the statement in the THEN clause cannot be followed with a semicolon (;), since that completes the IF statement and separates it from the following statement. The following examples contain correct code:


IF x > 10 THEN y := 4           IF x > 10 THEN BEGIN y := 4;
          ELSE y := 5;                               z := 5;
                                               END
                                          ELSE y := 5;

The ELSE clause always modifies the closest IF-THEN statement. Use caution to avoid logic errors in nested IF statements, as in the following:


IF A = 1 THEN    {First IF}
   IF B<>1 THEN  {Second IF}
      C := 1
ELSE             {Appears to modify first IF}
   C := 0;       {Actually modifies second IF}

HP Pascal can not always evaluate all the terms of a Boolean expression if it can evaluate the entire expression based on the value of one term. Either do not write code that depends on actual evaluation (or evaluation order) of Boolean expressions, or use the AND_THEN and OR_ELSE operators for a predictable order of evaluation.

For More Information:

5.10 Procedure Call

Syntactically, a procedure call is a statement. A procedure call has the following form:


routine-identifier [[({actual-parameter},...)]]

routine-identifier

The name of a procedure or function.

actual-parameter

An expression that is of a type that is compatible with the type of the formal parameter, or the name of a procedure or function.

In HP Pascal, you can use procedure-call syntax to call a function, even though function calls are usually considered to be expressions. If you do this, the compiler invokes the function but ignores the return value.

For More Information:

On procedures and functions (Chapter 6)

5.11 REPEAT Statement

The REPEAT statement is a looping statement and executes one or more statements until a specified condition is true. A REPEAT statement has the following form:


    REPEAT
        {statement};...
    UNTIL expression

statement

Any Pascal statement.

expression

Any Boolean expression.

Pascal always executes a REPEAT statement for one iteration; iterations continue as long as the Boolean expression is FALSE. When specifying more than one statement as the loop body to a REPEAT statement, do not enclose the statements with the BEGIN and END reserved words. Multiple statements are legal in the REPEAT loop body.

Consider the following example:


REPEAT
   READ( x );     {Attempts to read at least one character}
   IF ( x IN ['0'..'9'] ) THEN
      BEGIN       {Keep count of numbers and increase total}
      Digit_Count := Digit_Count + 1;
      Digit_Sum := Digit_Sum + ORD( x ) - ORD( '0' );
      END
   ELSE
      Char_Count := Char_Count+1;   {Count characters}
UNTIL EOLN(INPUT); {Reads from default device until end of line}

For More Information:

5.12 RETURN Statement

The RETURN statement passes control back to the caller of a PROCEDURE, FUNCTION, PROGRAM, or module initialization or finalization section. A RETURN statement is equivalent to a GOTO to a label placed just before the END of the body, and in a PROGRAM, has the effect of stopping the program. A RETURN statement has the following form:


RETURN [ return-value ]

return-value

Inside a FUNCTION, return-value specifies an ending value for the FUNCTION. If no return-value is provided, the last value assigned to the function identifier is used as the function result. The return-value type and function type must be the same.

Inside a PROGRAM, the return-value specifies an ending value for the PROGRAM. If you do not provide a return-value, HP Pascal uses the value 1.

A function returns a result to its caller. A function can specify its result by assigning a value to the function identifier. This does not cause a return to the caller, and can occur many times per call. When the function finally returns, the last assignment becomes the result of the function. If a function uses the RETURN statement and includes a value, then the function returns immediately and returns the specified value. This value overrides any previous values specified by an assignment.

If a function uses the RETURN statement but does not specify a value, the return value is the last value that the function specified by assignment. If the function has not assigned a value to the function identifier during a given call to the function, HP Pascal does not define the function result.

The following example shows the usage of RETURN. Here, a function searches through the array called Data for an element that matches Suitable. When it finds one, it assigns values to two global variables and executes a RETURN. Omitting the RETURN statement would make the function continue processing; it would assign values for the last suitable element instead of the first.



FUNCTION FindFirst(StartingPoint: INTEGER) : INTEGER;
               VAR i: INTEGER;
               BEGIN
               FOR i := StartingPoint TO MaximumNumber DO
                 BEGIN
                 IF Data[i] = Suitable THEN
                  BEGIN
                  AttributesOfDesiredData = Attributes[i];
                  Subscript := i;
                  RETURN i;
                  END;
               END;
            END;

For More Information:

5.13 WHILE Statement

The WHILE statement is a loop that executes a statement while a specified condition is true. A WHILE statement has the following form:


         WHILE expression DO
            statement

expression

Any Boolean expression.

statement

Any Pascal statement.

Pascal checks the value of the Boolean expression before executing the loop body for the first time; if the expression is FALSE, the loop body is not executed. If the initial value is TRUE, loop iterations continue until the condition is FALSE. When specifying more than one statement as the loop body to a WHILE statement, enclose the statements with the BEGIN and END reserved words, since the syntax calls for a single statement to follow the DO reserved word. If you do not use a compound statement for the loop body, Pascal executes the first statement following the DO reserved word as the loop body.

Consider the following examples:


WHILE NOT EOF( File1 ) DO  {If EOF from the start, the loop}
   READLN( File1 );        {   body is not executed.       }

WHILE NOT EOLN( INPUT ) DO
   BEGIN                 {Use compound statement:}
   READ( x );
   IF NOT ( x IN ['A'..'Z', 'a'..'z', '0'..'9'] )
   THEN
      Err := Err + 1;    {Count odd characters as errors}
   END;

For More Information:

5.14 WITH Statement

The WITH statement provides an abbreviated notation for references to the fields of a record variable or to the formal discriminants of a discriminated schema type. A WITH statement has the following form:


                {record-variable  }
         WITH { {schema-variable  }},... DO statement

record-variable

The name of the record variable being referenced.

schema-variable

The name of the variable being referenced whose type is a discriminated schema type. This underlying type of the schema can be a record.

statement

Any Pascal statement.

The WITH statement allows you to refer to the fields of a record or to a formal discriminant of a schema by their names alone, rather than by the record.field-identifier or schema-variable.formal-discriminant syntax. In effect, the WITH statement opens the scope so that references to field identifiers or to formal discriminants alone are unambiguous. When you access a variable or one of its components using a WITH statement, the reference syntax lasts throughout the execution of the statement.

Specifying more than one variable has the same effect as nesting WITH statements. Consider the following example:


{The record Dog is nested in the record Cat:}
WITH Cat, Dog DO    {Specify Cat before Dog}
   Bills := Bills + Cat_Vet + Dog_Vet;

WITH Cat DO   {This is equivalent to the previous WITH}
     WITH Dog DO
          Bills := Bills + Cat_Vet + Dog_Vet;

If you are specifying nested records, their variable names must appear in the order in which they were nested in the record type definition. If you are working with record and schema variables that are not nested, you can specify variable names in any order. If you specify record or schema variables whose field names or formal discriminants conflict with one another, Pascal uses the last record or schema in the comma list. Consider the following example.


VAR
   x : STRING( 10 );
   y : STRING( 15 );

{In the executable section:}
WITH x, y DO
   WRITELN( CAPACITY );       {y.CAPACITY is used}

{The following is equivalent:}
WITH x DO
   WITH y DO
      WRITELN( CAPACITY );

For More Information:


Chapter 6
Procedures and Functions

This chapter discusses the following information about user-defined routines:

Procedures and functions are subprograms. 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. This manual refers to functions and procedures collectively as routines.

In addition to user-defined routines, HP Pascal also allows you to access external routines (routines that are globally available on your system and that may or may not be written in HP Pascal) and routines that are predeclared by the compiler.

For More Information:

  • On predeclared routines (Chapter 8)
  • On calling external routines (HP Pascal for OpenVMS User Manual)

6.1 Routine Declarations

You must declare a routine before you call it. Routine declarations have the following formats:


         [[attribute-list]] PROCEDURE routine-identifier [[(formal-parameter-list)]];
             {[[declaration-section]] BEGIN {statement};... END  }
             {{ EXTERN    }                                      }
             {{ EXTERNAL  }                                      }
             {{           }                                      } ;
             {{ FORTRAN   }                                      }
             {{ FORWARD   }                                      }

         [[attribute-list]] FUNCTION routine-identifier [[(formal-parameter-list)]]
            : [[attribute-list]] result-type-id;
             {[[declaration-section]] BEGIN {statement};... END  }
             {{ EXTERN    }                                      }
             {{ EXTERNAL  }                                      }
             {{           }                                      } ;
             {{ FORTRAN   }                                      }
             {{ FORWARD   }                                      }
             {                                                   }

attribute-list

One or more identifiers that provide additional information about the type-denoter.

routine-identifier

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.

formal-parameter-list

A comma list of the routine's formal parameters. A procedure can have as many as 255 formal parameters. A function can also have as many as 255 formal parameters unless returning a structured type, in which case a function is limited to 254 formal parameters. Optionally, you can specify a mechanism specifier and an attribute list for each parameter.

declaration-section

A routine 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.

By default, the system does not retain the values of local variables after it exits from a routine. Each call to a routine creates copies of the local variables. This means you can call a routine recursively without affecting the values held by the local variables at each activation of the routine. To preserve the value of a local variable (not the copy) from one call to the next, you must declare the local variable with the STATIC attribute.

statement

Any Pascal statement. In a function executable section, there must be either a RETURN statement containing a value, or 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 HP Pascal returns when the function is called. (This value cannot be a file type or a structured type with a file component.) This value must be of the same type as the result-type-id.

EXTERN

EXTERNAL

FORTRAN

FORWARD

Predeclared identifiers that direct HP Pascal to find the body of the routine elsewhere. The EXTERN, EXTERNAL, and FORTRAN identifiers declare routines that are independently compiled by HP Pascal or that are written in other languages. In HP 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 FORWARD identifier declares 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, HP 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.

result-type-id

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.

Consider the following example:


{Function body contained in subsequent code:}
FUNCTION Adder( Op1, Op2, Op3 : REAL ) : REAL; FORWARD;

PROCEDURE Introduction;
VAR
    a, b, c, z : REAL;    {Variables local to the procedure}
BEGIN
WRITELN( 'This is the Inventory Program Version 5.6.' );
WRITELN;
WRITELN( 'Press Ctrl/H for help.  Press Return to continue.' );
a := 4.6;   b := 12.1;   c := 201.45;
z := Adder( a, b, c );    {Call the function Adder}
END;

{System_Routine_Tanh available with the operating system:}
FUNCTION System_Routine_Tanh( Angle : REAL ) : REAL;

EXTERNAL;


FUNCTION Adder;  {Do not repeat attributes or parameters}
BEGIN
Adder := Op1 + Op2 + Op3;   {Assign a function return value}
END;

For More Information:

  • On attributes (Chapter 10)
  • On declaration sections (Chapter 3)
  • On the scope of identifiers ( Section 7.2)
  • On parameters and passing mechanisms ( Section 6.3)
  • On recursive function calls (HP Pascal for OpenVMS User Manual)
  • On calling external routines (HP Pascal for OpenVMS User Manual)


Previous Next Contents Index