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

From Lazarus wiki
Jump to navigationJump to search
m
m (Fixed syntax highlighting; deleted category included in page template)
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Forward Referencing}}
 
{{Forward Referencing}}
  
4F - 向前引用 (原作者: Tao Yue, 状态: 未更改)
+
4F - 前向引用 (原作者: Tao Yue, 状态: 未更改)
  
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.
+
不过,也有例外,你想先使用后定义?要解决这类问题,你需要使用''前向引用''。(在预先声明的过程/函数后加''forward'')
  
To resolve this chicken-and-the-egg problem, use ''forward referencing''.
+
如:
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
procedure Later (parameter list); forward;
+
procedure Later (参数列表); forward;
  
procedure Sooner (parameter list);
+
procedure Sooner (参数列表);
 
begin
 
begin
 
   ...
 
   ...
   Later (parameter list);
+
   Later (参数列表);
 
end;
 
end;
 
...
 
...
Line 22: Line 22:
 
begin
 
begin
 
   ...
 
   ...
   Sooner (parameter list);
+
   Sooner (参数列表);
 
end;
 
end;
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The same goes for functions. Just stick a forward; at the end of the heading.
+
 
 +
示例:
 +
<syntaxhighlight lang=pascal>
 +
program q;
 +
 
 +
procedure a;forward;
 +
 
 +
procedure b;
 +
begin
 +
    writeln('B调取A:');
 +
    a;
 +
end;
 +
 
 +
procedure a;
 +
begin
 +
    writeln('A过程.');
 +
end;
 +
 
 +
begin
 +
    b;
 +
end.
 +
</syntaxhighlight>
 +
 
 +
=== 相关 ===
 +
[https://zh.wikipedia.org/wiki/%E5%89%8D%E5%90%91%E5%A3%B0%E6%98%8E 维基百科前向声明]
  
 
{|style=color-backgroud="white" cellspacing="20"
 
{|style=color-backgroud="white" cellspacing="20"
|[[Recursion/zh_CN|上一页]]
+
|[[Recursion/zh_CN|上一页]]
|[[Contents/zh_CN|目录]]  
+
|[[Contents/zh_CN|目录]]
 
|[[Programming_Assignment_4/zh_CN|下一页]]
 
|[[Programming_Assignment_4/zh_CN|下一页]]
 
|}
 
|}
 
[[Category: Object Pascal Introduction]]
 
[[Category:zh]]
 

Revision as of 00:57, 16 February 2020

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

4F - 前向引用 (原作者: Tao Yue, 状态: 未更改)

这些虽然容易理解,但有时也让人困惑。

程序中只能使用已经定义的过程/函数?或许是这样吧,先定义后使用。

不过,也有例外,你想先使用后定义?要解决这类问题,你需要使用前向引用。(在预先声明的过程/函数后加forward)

如:

procedure Later (参数列表); forward;

procedure Sooner (参数列表);
begin
  ...
  Later (参数列表);
end;
...
procedure Later;
begin
  ...
  Sooner (参数列表);
end;


示例:

program q;

procedure a;forward;

procedure b;
begin
    writeln('B调取A:');
    a;
end;

procedure a;
begin
    writeln('A过程.');
end;

begin
    b;
end.

相关

维基百科前向声明

上一页 目录 下一页