Page 1 of 1

Forward Procedures

Posted: Mon Jan 03, 2011 11:06 pm
by Helpdesk
How do I declare a forward Procedure? The declaration:

Code: Select all

PROCEDURE ^P(i: INTEGER);
Results in the error message "no ;" at the ^.

Re: Forward Procedures

Posted: Mon Jan 03, 2011 11:19 pm
by cfbsoftware
There is no special mechanism used for forward procedures in Oberon-07. Normally procedures should be declared before they are referenced. The only time where it is necessary to use a forward procedure is if two procedures mutually call each other e.g. if you want to do something like:

Code: Select all

PROCEDURE ProcA(a: INTEGER);
BEGIN
  ...
  ProcB(a-1);
  ...
END ProcA;

PROCEDURE ProcB(a: INTEGER);
BEGIN
  ...
  ProcA(a-1);
  ...
END ProcB;
There are two ways you can achieve this:

a) Nest one procedure inside the other:

Code: Select all

PROCEDURE ProcA(a: INTEGER);

  PROCEDURE ProcB(a: INTEGER);
  BEGIN
    ...
    ProcA(a-1);
    ...
  END ProcB;

BEGIN
  ...
  ProcB(a-1);
  ...
END ProcA;
b) Declare one of the procedures as procedure variable:

Code: Select all

VAR
  procVarB: PROCEDURE(a: INTEGER);

PROCEDURE ProcA(a: INTEGER);
BEGIN
  ...
  procVarB(a-1);
  ...
END ProcA;

PROCEDURE ProcB(a: INTEGER);
BEGIN
  ...
  ProcA(a-1);
  ...
END ProcB;

BEGIN
  procVarB := ProcB
END.