このページには最新のブラウザ(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 かつ 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 2つ 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.
実行ボタンをクリックして出力を確認してください。