Basic Pascal Tutorial/Chapter 4/Forward Referencing

This is an old revision of Basic Pascal Tutorial/Chapter 4/Forward Referencing, edited by Kees at . The current revision is 47008.

New page: 4F - Forward Referencing After all these confusing topics, here's something easy. Remember that procedures/functions can only see variables and other subprograms that have already been d...

4F - Forward Referencing

After all these confusing topics, here's something easy.

Remember that procedures/functions can only see variables and other subprograms that have already been defined? Well, there is an exception.

If you have two subprograms, each of which calls the other, you have a dilemma that no matter which you put first, the other still can't be called from the first.

To resolve this chicken-and-the-egg problem, use forward referencing.

   1: procedure Later (parameter list); forward;
   2: 
   3: procedure Sooner (parameter list);
   4: begin
   5:   ...
   6:   Later (parameter list);
   7: end;
   8: ...
   9: procedure Later;
  10: begin
  11:   ...
  12:   Sooner (parameter list);
  13: end;

The same goes for functions. Just stick a forward; at the end of the heading.

previous contents next