Contents | Prev | Next | Index


Procedure Declarations

A procedure declaration consists of a procedure heading and a procedure body. The heading specifies the procedure identifier and the formal parameters. For type-bound procedures it also specifies the receiver parameter. The body contains declarations and statements. The body is a block which is local to the procedure. The procedure identifier is repeated at the end of the procedure declaration. 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 Java specific extensions are shown in the following box in red color.

procedure_decl ::= procedure_heading semicolon block <identifier>
               ::= procedure_heading semicolon FORWARD
semicolon      ::= ;
               ::= ; java_throws

procedure_heading ::= procedure proc_id
                  ::= procedure proc_id formal_parameters
procedure         ::= modifier PROCEDURE
modifier          ::= java_modifiers
                  ::= <empty>

proc_id           ::= <identifier>
                  ::= receiver <identifier>
                  ::= <identifier> [ <string> ]
                  ::= receiver <identifier> [ <string> ]
                  ::= <identifier> java_name
                  ::= receiver <identifier> java_name

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 procedure specifies a result type in its formal parameter list. Also, the body of a function procedure must have a return statement that defines the result.

The procedure body is a block which declares constants, variables, types, and procedures. These declarations are local to the procedure. Procedure declarations may be nested. The procedure 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 in which they are not hidden by locally declared items with same name.

If a procedure declaration specifies a receiver parameter, the procedure is considered to be bound to a type (see type-bound procedures). They are called methods in conventional OOP terminology. This feature is only available as part of the language extensions which can be enabled via a compiler switch or directive.

A forward declaration has the purpose to allow forward references to a procedure whose actual declaration appears later in the same block. The formal parameter lists of the forward declaration and the actual declarations must match.

The following are examples of procedure declarations:

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

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 WriteInt

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

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


Contents | Prev | Next | Index

Canterbury Modula-2 for Java  (Last documentation update Feb 8, 2000)
Copyright © 1998 Mill Hill & Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to mhc@webcom.com