此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
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.
English
Make a procedure
- Start with
def, then a name, then(). - Put the code indented below.
- Call the procedure by writing its name with
().

English
def greet():
print("Hello!")
greet()
greet()
English
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.
English
def greet(name):
print("Hello, " + name)
greet("Mia")
greet("Sam")
English
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.
English
def square(n):
return n * n
answer = square(5)
print(answer)
English
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.
English
In AP CSP pseudocode
- The exam uses a pseudocode for procedures.
PROCEDUREis likedef;RETURNis likereturn.
PROCEDURE square(n)
{
RETURN(n * n)
}
English
Common mistakes
- A procedure with a
returngives back a value; call it with(). - Abstraction hides the detail behind a simple name.
English
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.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Write a procedure square(n) that 返回值 n multiplied by itself. Do not print inside it — use return.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Write a procedure area(width, height) with 二 parameters that 返回值 the area (width times height).
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Write to_seconds(minutes, seconds) that 返回值 the total number of seconds. (One minute is 60 seconds.) Then it combines a multiply step and an add step.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The procedure double(x) is given. 调用 it with 21 and store the result in a variable named result.
点击“运行”查看此处输出。