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

From Lazarus wiki
Jump to navigationJump to search
m
m (Fixed syntax highlighting)
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
{{Forward Referencing}}
 
{{Forward Referencing}}
 +
{{TYNavigator|Recursion|Programming_Assignment_4}}
  
 
4F - Forward Referencing (author: Tao Yue, state: unchanged)
 
4F - Forward Referencing (author: Tao Yue, state: unchanged)
Line 10: Line 11:
  
 
To resolve this chicken-and-the-egg problem, use ''forward referencing''.
 
To resolve this chicken-and-the-egg problem, use ''forward referencing''.
<syntaxhighlight>
+
 
 +
<syntaxhighlight lang=pascal>
 
procedure Later (parameter list); forward;
 
procedure Later (parameter list); forward;
  
Line 28: Line 30:
 
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.
  
{|style=color-backgroud="white" cellspacing="20"
+
{{TYNavigator|Recursion|Programming_Assignment_4}}
|[[Recursion|previous]] 
 
|[[Contents|contents]]
 
|[[Programming_Assignment_4|next]]
 
|}
 
 
 
[[Category: Object Pascal Introduction]]
 

Revision as of 00:56, 16 February 2020

български (bg) English (en) français (fr) 日本語 (ja) 中文(中国大陆)‎ (zh_CN)

 ◄   ▲   ► 

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.

procedure Later (parameter list); forward;

procedure Sooner (parameter list);
begin
  ...
  Later (parameter list);
end;
...
procedure Later;
begin
  ...
  Sooner (parameter list);
end;

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

 ◄   ▲   ►