Calling Procedures
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| reusable/riːˈjuːzəbl/ | có thể tái sử dụng |
| procedure/prəˈsiːdʒə/ | thủ tục |
| call/kɔːl/ | gọi |
| method/ˈmeθəd/ | phương thức |
| arguments/ˈɑːɡjuːmənts/ | tham số thực |
| parameters/pəˈræmɪtəz/ | tham số hình thức |
| return value/rɪˈtɜːn ˈvæljuː/ | giá trị trả về |
| implementation/ˌɪmplɪmənˈteɪʃn/ | cài đặt |
| abstraction/əbˈstrækʃn/ | Trừu tượng hóa |
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".
A procedure (function/method) is:
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Write it once, call it many times.
To run a procedure's instructions from elsewhere, you ______ it by name.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
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.

Arguments 实参 are passed in, the body runs, and RETURN sends a value back
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.
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Arguments are the actual values; parameters are the named slots.
In total ← area(4, 5), what does area give back?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
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.
If square(n) returns n × n, what does square(square(3)) return?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
square(3) = 9, then square(9) = 81.
To call square(3), you must know exactly how it works inside.
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
False. Procedural abstraction means you only need to know what the procedure does, not how it is implemented.
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.
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.