Procedures and BYREF · 过程与 BYREF
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
CALL runs a procedure
- A procedure is a named block. It does a job and returns no value.
- Define it with
PROCEDURE…ENDPROCEDURE, and run it withCALL. - Parentheses are written even when there are no parameters.
中文
CALL 运行一个过程
- 过程是一段有名字的程序。它完成一项工作,不返回值。
- 用
PROCEDURE…ENDPROCEDURE定义,用CALL运行。 - 即使没有参数,也要写括号。
PROCEDURE Greet()
OUTPUT "Hi"
ENDPROCEDURE
CALL Greet()
English
By value is the default
- A parameter with no mode is passed by value: the procedure gets a copy.
- Changing the copy does not change the caller's variable.
- You may also write
BYVALin front of the name. It means the same thing.
中文
默认按值传递
- 没有标明方式的参数按值传递:过程得到的是副本。
- 改副本不会改调用者的变量。
- 也可以在名字前写
BYVAL。意思相同。
PROCEDURE Show(N : INTEGER)
N ← N + 1
OUTPUT N
ENDPROCEDURE
DECLARE A : INTEGER
A ← 5
CALL Show(A)
OUTPUT A
English
BYREF passes the real variable
BYREFmeans the parameter is the caller's variable. A change is still there afterCALL.- If several parameters in a row use the same mode, the guide lets you write the keyword once.
- Do not pass a parameter to a function by reference. Use
BYREFon a procedure.
中文
BYREF 传递真正的变量
BYREF表示这个参数就是调用者的变量。CALL返回后,改动还在。- 连续几个参数用同一种方式时,指南允许关键字只写一次。
- 不要把参数按引用传给函数。
BYREF用在过程上。
PROCEDURE Swap(BYREF X : INTEGER, BYREF Y : INTEGER)
DECLARE Temp : INTEGER
Temp ← X
X ← Y
Y ← Temp
ENDPROCEDURE
DECLARE A : INTEGER
DECLARE B : INTEGER
A ← 1
B ← 2
CALL Swap(A, B)
OUTPUT A & "," & B
English
Common mistakes
CALLbefore a procedure. NeverCALLa function.- IGCSE 0478 has no
BYREF. On that paper every parameter is a copy.
中文
常见错误
- 过程前面要写
CALL。函数前面绝不写CALL。 - IGCSE 0478 没有
BYREF。那张试卷上每个参数都是副本。
English
Now you try
- Write a procedure that swaps two integers.
中文
现在你来试
- 写一个交换两个整数的过程。
Write a procedure Greet with no parameters that outputs Hi, then CALL it. · 写一个没有参数的过程 Greet,它输出 Hi,然后 CALL 它。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write Swap so that BYREF exchanges A and B. Start from A ← 1 and B ← 2, and output 2,1. · 写 Swap,用 BYREF 交换 A 和 B。从 A ← 1、B ← 2 开始,输出 2,1。
Click Run to see the output here. · 点击“运行”查看此处输出。