Procedures and abstraction · 过程与抽象
What we'll do
- A procedure is a named block of code you can use again and again.
- In Python a procedure is made with
def. - This is part of Big Idea 3: building programs from reusable parts.
我们要做什么
- 过程(procedure)是一段有名字的代码,你可以反复使用它。
- 在 Python 里,过程用
def来创建。 - 这是大概念 3 的一部分:用可复用的部件来搭建程序。
Make a procedure
- Start with
def, then a name, then(). - Put the code indented below.
- Call the procedure by writing its name with
().
创建一个过程
- 先写
def,然后是名字,再加()。 - 把代码缩进写在下面。
- 写出过程的名字加
()就可以调用它。
def greet():
print("Hello!")
greet()
greet()
Parameters pass data in
- A parameter is a value the procedure receives.
- Put the parameter name inside the
(). - Inside the procedure you use it like a variable.
参数把数据传进来
- 参数(parameter)是过程接收到的一个值。
- 把参数名字写在
()里面。 - 在过程内部,你像使用变量一样使用它。
def greet(name):
print("Hello, " + name)
greet("Mia")
greet("Sam")
return gives a value back
returnsends a value out of the procedure.- The caller can store that value or use it.
- After
return, the procedure stops.
return 把值送回来
return把一个值送出过程。- 调用者可以存储这个值或者使用它。
- 执行到
return之后,过程就停止。
def square(n):
return n * n
answer = square(5)
print(answer)
Why procedures help: abstraction
- Abstraction means hiding the details behind a simple name.
- You call
square(5)and do not think about how it works inside. - This makes code shorter, clearer, and easy to reuse.
为什么过程有用:抽象
- 抽象(abstraction)是指把细节藏在一个简单的名字后面。
- 你调用
square(5),不用去想它内部是怎么运作的。 - 这让代码更短、更清楚,也容易复用。
In AP CSP pseudocode
- The exam uses a pseudocode for procedures.
PROCEDUREis likedef;RETURNis likereturn.
用 AP CSP 伪代码表示
- 考试使用一种伪代码来表示过程。
PROCEDURE就像def;RETURN就像return。
PROCEDURE square(n)
{
RETURN(n * n)
}
Common mistakes
- A procedure with a
returngives back a value; call it with(). - Abstraction hides the detail behind a simple name.
常见错误
- 带
return的过程会返回一个值;用()调用它。 - 抽象把细节藏在一个简单的名字后面。
Now you try
- Read each task to see the procedure name and what it must return.
- Press Check answer to test your code.
现在轮到你
- 读每个任务,看清过程的名字以及它必须返回什么。
- 按检查答案来测试你的代码。
A procedure call · 过程调用
Calling a procedure passes arguments in and returns a value out. · 调用过程时把参数传进去,再返回一个值。
Write a procedure square(n) that returns · 返回值 n multiplied by itself. Do not print inside it — use return. · 写一个过程 square(n),返回 n 乘以它自己。不要在里面打印 —— 用 return。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write a procedure area(width, height) with two parameters that returns · 返回值 the area (width times height). · 写一个过程 area(width, height),它有两个参数,返回面积(宽乘以高)。
Click Run to see the output here. · 点击“运行”查看此处输出。
Write to_seconds(minutes, seconds) that returns · 返回值 the total number of seconds. (One minute is 60 seconds.) Then it combines a multiply step and an add step. · 写一个 to_seconds(minutes, seconds),返回总秒数。(一分钟是 60 秒。)它把一个乘法步骤和一个加法步骤组合在一起。
Click Run to see the output here. · 点击“运行”查看此处输出。
The procedure double(x) is given. Call · 调用 it with 21 and store the result in a variable named result. · 已经给好了过程 double(x)。用 21 调用它,并把结果存进一个名为 result 的变量。
Click Run to see the output here. · 点击“运行”查看此处输出。