Forward Procedures

Topics related to the use of Oberon language features
Post Reply
Helpdesk
Posts: 33
Joined: Sat Jan 01, 2011 5:43 am
Contact:

Forward Procedures

Post by Helpdesk » Mon Jan 03, 2011 11:06 pm

How do I declare a forward Procedure? The declaration:

Code: Select all

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

cfbsoftware
Site Admin
Posts: 493
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Forward Procedures

Post by cfbsoftware » Mon Jan 03, 2011 11:19 pm

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. 

Post Reply