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.
行うこと
- プロシージャーは名前赋予了コードのブロックで、再利用できます。
- Pythonではプロシージャーを
defで作成します。 - これはBig Idea 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.
パラメータはデータを渡す
- パラメータはプロシージャーが受け取る値です。
- パラメータ名を
()の中に書きます。 - プロシージャー内では変数として使用します。
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.
プロシージャーの利点:抽象化
- 抽象化とは、単純な名前の背後に詳細を隠すことです。
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). · 2つ のパラメータを持つ 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) を書きます。(1分は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. · 実行ボタンをクリックして出力を確認してください。