Calling Procedures · 调用过程
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| reusable/riːˈjuːzəbl/ | 可重用 | kě zhòng yòng |
| procedure/prəˈsiːdʒə/ | 过程 | guò chéng |
| call/kɔːl/ | 调用 | diào yòng |
| method/ˈmeθəd/ | 方法 | fāng fǎ |
| arguments/ˈɑːɡjuːmənts/ | 实参 | shí cān |
| parameters/pəˈræmɪtəz/ | 形参 | xíng cān |
| return value/rɪˈtɜːn ˈvæljuː/ | 返回值 | fǎn huí zhí |
| implementation/ˌɪmplɪmənˈteɪʃn/ | 实现 | shí xiàn |
| abstraction/əbˈstrækʃn/ | 抽象 | chōu xiàng |
Reusable blocks of code
- A procedure 过程, also called a function or method 方法, is a named block of reusable 可重用 code.
- You write it once and use it many times.
- It gives a job a name, so you can invoke that job whenever you need it.
- This is the heart of "don't repeat yourself".
可重用的代码块
- 一个过程(procedure),也叫函数(function)或方法(method),是一个命名的可重用(reusable)代码块。
- 你写它一次,用它许多次。
- 它给一个任务一个名字,于是你能在需要时调用那个任务。
- 这是"不要重复自己"的核心。
A procedure (function/method) is: · 一个过程(函数/方法)是:
Write it once, call it many times. · 写一次,调用多次。
To run a procedure's instructions from elsewhere, you ______ it by name. · 要从别处运行一个过程的指令,你按名字______它。
After the call finishes, the program continues where it left off. · 调用完成后,程序从它离开的地方继续。
Calling a procedure
- To run a procedure from elsewhere, you call 调用 it by name.
- When the call finishes, the program continues from where it left off.
- So a call is like a detour: go do the job, then come back.
- The same procedure can be called from many places.
调用过程
- 要从别处运行一个过程,你按名字调用(call)它。
- 当调用完成,程序从它离开的地方继续。
- 所以调用像一段绕路:去做那个任务,然后回来。
- 同一个过程能从许多地方被调用。
How a procedure call works · 一个过程调用如何工作
A call jumps to the procedure with its arguments, runs the body, returns a value, and the program continues where it left off. · 一个调用带着实参跳到过程,运行主体,返回一个值,程序从它离开的地方继续。
Match each term in area(4, 5) to its meaning. · 把 area(4, 5) 中的每个词与其含义配对。
Arguments are the actual values; parameters are the named slots. · 实参是实际的值;形参是命名的位置。
In total ← area(4, 5), what does area give back? · 在 total ← area(4, 5) 中,area 返回什么?
The returned result is stored for later use. · 返回的结果被存储供以后用。
Arguments, parameters, return
- Procedures can take inputs: you pass arguments 实参 into a procedure through its parameters 形参.
area(length, width)called asarea(4, 5)receives 4 and 5.- A procedure can also give back a return value 返回值 — a result you use later.
- For example
total ← area(4, 5)stores the returned value.
实参、形参、返回
- 过程能接收输入:你通过它的形参(parameters)把实参(arguments)传进一个过程。
area(length, width)以area(4, 5)调用时接收 4 和 5。- 一个过程也能返回一个返回值(return value)——一个你以后用的结果。
- 例如
total ← area(4, 5)存储返回的值。
If square(n) returns n × n, what does square(square(3)) return? · 如果 square(n) 返回 n × n,square(square(3)) 返回什么?
square(3) = 9, then square(9) = 81. · square(3) = 9,然后 square(9) = 81。
When you call square(3), you need to know what it does, not how it works inside. · 当你调用 square(3),你需要知道它做什么,而非它内部如何工作。
Hiding implementation detail is procedural abstraction. · 隐藏实现细节是过程抽象。
Hiding implementation
- Procedures support abstraction 抽象 by hiding implementation 实现 detail from the caller.
- You only need to know what a procedure does, not how it works inside.
square(n) returns n × n. a ← square(3) stores 9. b ← square(a) stores 81, because square(9) is 81. The caller never sees the multiplication inside — it just uses the return value.
隐藏实现
- 过程通过对调用者隐藏实现(implementation)细节支持抽象(abstraction)。
- 你只需知道一个过程做什么,而非它内部如何工作。
square(n) 返回 n × n。 a ← square(3) 存 9。b ← square(a) 存 81,因为 square(9) 是 81。调用者从不看到内部的乘法——它只用返回值。
A procedure (a function or method) is reusable code you call by name; the program continues after it returns. You pass arguments into its parameters and can use its return value. Procedures give abstraction — the caller knows what it does, not the implementation.
一个过程(函数或方法)是你按名字调用的可重用代码;程序在它返回后继续。你把实参传进它的形参,并能用它的返回值。过程给出抽象——调用者知道它做什么,而非实现。