Contents | Prev | Next | Index


Procedure Declarations

A procedure declaration consists of a procedure heading or a function heading and a procedure body. The heading specifies the procedure identifier and the formal parameters. The body is a subroutine containing declarations and statements. Or it can be forward declared. It is a block which is local to the procedure. In this compiler, the procedure declaration may be preceded by a Java modifiers directive, and it may be followed by a Java Throws directive. Also, the procedure identifier may have a different Java identifier. The following syntax definition shows Java-specific extensions in red color, umimplemented or obsolete features are shown in green color.

procedure_decl    ::= procedure_heading semicolon subroutine
                  ::= class_directive
procedure_heading semicolon subroutine
func_decl ::= func_heading semicolon subroutine ::= class_directive
func_heading semicolon subroutine
class_directive ::= CLASS semicolon ::= ; ::= ; java_throws procedure_heading ::= procedure proc_id ::= procedure proc_id formal_parameters ::= constructor proc_id ::= constructor proc_id formal_parameters ::= destructor proc_id ::= destructor proc_id formal_parameters procedure ::= modifier PROCEDURE constructor ::= modifier CONSTRUCTOR destructor ::= modifier DESTRUCTOR modifier ::= java_modifiers ::= <empty> proc_id ::= ident ::= ident . member_id ::= ident [ <string> ] ::= ident . member_id [ <string> ] ::= ident java_name ::= ident . member_id java_name formal_parameters ::= ( fp_section_list ) fp_section_list ::= fp_section_list ; fp_section ::= fp_section fp_section ::= VAR ident_list : formal_type ::= ident_list : formal_type ::= CONST ident_list : formal_type ::= VAR ident_list formal_type ::= qualident ::= open_array qualident ::= open_array tvarrec ::= STRING ::= open_array STRING ::= FILE ::= open_array FILE open_array ::= ARRAY OF tvarrec ::= CONST func_heading ::= function proc_id func_parameters function ::= modifier FUNCTION func_parameters ::= : result_type ::= ( fp_section_list ) : result_type result_type ::= qualident ::= STRING subroutine ::= param_directive block ::= param_directive FORWARD ::= param_directive external param_directive ::= REGISTER ; ::= PASCAL ; ::= CDECL ; ::= STDCALL ; ::= <empty> external ::= EXTERNAL ::= EXTERNAL <string> ::= EXTERNAL <string> NAME <string> ::= EXTERNAL <string> INDEX <integer>

There are two kinds of procedures: proper procedures and function procedures. The latter are activated by a function designator as part of an expression, and they yield a result that is an operand of an expression. Proper procedures are acitvated by a procedure call. A function specifies a result type in its formal parameter list. Also, the body of a function must assign variable of the same name as the function identifier or of the name "Result" to a return value which is assignment compatible with the result type.

The procedure or function body is a block which declares constants, variables, types, and procedures. These declarations are local to the procedure. Procedure or function declarations may be nested. The procedure or function may also be called from within its declaration which results in a recursive activation.

Items declared in the environment of the procedure are also visible in those parts of the procedure or function in which they are not hidden by locally declared items with same name.

A procedure identifier may be preceded by another identifier and a period. In that case the procedure declaration refers to a implementing method declaration, with the first identifier refering to the class type and the second identifier refering to the method member name.

A forward declaration has the purpose to allow forward references to a procedure or function whose actual declaration appears later in the same block. The actual declaration may omit or repeat the formal parameter list. If repeated, the parameters of the forward declaration and the actual declarations must match.

The following are examples of procedure and function declarations:

PROCEDURE ReadInt( VAR x : INTEGER );
  VAR i : INTEGER;
  ch : CHAR;
BEGIN
  WHILE ('0' <= ch) AND (ch <= '9') DO BEGIN
    i := 10*i + (ORD(ch)-ORD('0'));
    Read(ch)
  END;
  x := i;
END;

PROCEDURE WriteInt( x : INTEGER );
  (*0 <= x < 100000*)
  VAR i : INTEGER;
  buf : ARRAY [0..4] OF INTEGER;
BEGIN
  REPEAT
    buf[i] := x MOD 10;
    x := x DIV 10;
    INC(i)
  UNTIL x=0;
  REPEAT
    DEC(i);
    Write(CHR(buf[i] + ORD('0')))
  UNTIL i=0;
END;

PROCEDURE WriteString( s : ARRAY OF CHAR );
  VAR i : INTEGER;
BEGIN
  i := 0;
  WHILE (i < HIGH(s)) AND (s[i] <> #0) DO BEGIN
    Write(s[i]);
    INC(i)
  END
END;

FUNCTION log2( x : INTEGER ) : INTEGER;
  VAR y : INTEGER; (*assume x>0*)
BEGIN
  y := 0;
  WHILE x>1 DO BEGIN
    x := x DIV 2;
    INC(y)
  END;
  log2 := y
END;


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com