Difference between revisions of "Basic Pascal Tutorial/Chapter 4/Forward Referencing"

From Lazarus wiki
Jump to navigationJump to search
Line 8: Line 8:
  
 
To resolve this chicken-and-the-egg problem, use ''forward referencing''.
 
To resolve this chicken-and-the-egg problem, use ''forward referencing''.
 +
<delphi>
 +
procedure Later (parameter list); forward;
  
<font color="#000000">  1:</font> <font color="#006699"><strong>procedure</strong></font> Later <font color="#000000"><strong>(</strong></font>parameter list<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font> <font color="#009966"><strong>forward</strong></font><font color="#000000"><strong>;</strong></font>
+
procedure Sooner (parameter list);
<font color="#000000">  2:</font>
+
begin
<font color="#000000">  3:</font> <font color="#006699"><strong>procedure</strong></font> Sooner <font color="#000000"><strong>(</strong></font>parameter list<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
   ...
<font color="#000000">  4:</font> <font color="#006699"><strong>begin</strong></font>
+
   Later (parameter list);
<font color="#990066">  5:</font>   <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
end;
<font color="#000000">  6:</font>   Later <font color="#000000"><strong>(</strong></font>parameter list<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
...
<font color="#000000">  7:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
+
procedure Later;
<font color="#000000">  8:</font> <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
begin
<font color="#000000">  9:</font> <font color="#006699"><strong>procedure</strong></font> Later<font color="#000000"><strong>;</strong></font>
+
   ...
<font color="#990066">  10:</font> <font color="#006699"><strong>begin</strong></font>
+
   Sooner (parameter list);
<font color="#000000">  11:</font>   <font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font><font color="#000000"><strong>.</strong></font>
+
end;
<font color="#000000">  12:</font>   Sooner <font color="#000000"><strong>(</strong></font>parameter list<font color="#000000"><strong>)</strong></font><font color="#000000"><strong>;</strong></font>
+
</delphi>
<font color="#000000">  13:</font> <font color="#006699"><strong>end</strong></font><font color="#000000"><strong>;</strong></font>
 
  
 
The same goes for functions. Just stick a forward; at the end of the heading.
 
The same goes for functions. Just stick a forward; at the end of the heading.

Revision as of 17:45, 5 January 2010

4F - Forward Referencing (author: Tao Yue, state: unchanged)

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. <delphi> procedure Later (parameter list); forward;

procedure Sooner (parameter list); begin

 ...
 Later (parameter list);

end; ... procedure Later; begin

 ...
 Sooner (parameter list);

end; </delphi>

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

previous contents next