Calling Procedures
Introduced| English |
|---|
| reusable/riːˈjuːzəbl/ |
| procedure/prəˈsiːdʒə/ |
| call/kɔːl/ |
| method/ˈmeθəd/ |
| arguments/ˈɑːɡjuːmənts/ |
| parameters/pəˈræmɪtəz/ |
| return value/rɪˈtɜːn ˈvæljuː/ |
| implementation/ˌɪmplɪmənˈteɪʃn/ |
| abstraction/əbˈstrækʃn/ |
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:
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.
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.
Arguments are the actual values; parameters are the named slots.
In total ← area(4, 5), what does area give back?
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?
square(3) = 9, then square(9) = 81.
When you call square(3), you need to know what it does, not how it works inside.
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.
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.