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.
สิ่งที่เราจะทำ
- A procedure คือบล็อกโค้ดที่มีชื่อซึ่งคุณสามารถใช้ซ้ำได้
- ใน Python procedure สร้างด้วย
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
().
สร้าง procedure
- เริ่มต้นด้วย
def, ตามด้วย ชื่อ, แล้ว() - วางโค้ด indented ด้านล่าง
- เรียก procedure โดยเขียนชื่อกับ
()
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.
Parameters ส่งข้อมูลเข้า
- A parameter คือค่าที่ procedure รับ
- วางชื่อ parameter ภายใน
() - ภายใน procedure ใช้เหมือนตัวแปร
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/value กลับมา
returnส่งค่า ออก จาก procedure- ผู้เรียกสามารถเก็บค่านั้นหรือใช้งานมันได้
- หลัง
return, procedure หยุดทำงาน
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.
ทำไม procedures ช่วย: abstraction
- Abstraction means hiding the details behind a simple name
- คุณเรียก
square(5)และไม่คิด关于วิธีทำงานภายใน - ทำให้โค้ดสั้นลง ชัดเจน และง่ายต่อการ reuse
In AP CSP pseudocode
- The exam uses a pseudocode for procedures.
PROCEDUREis likedef;RETURNis likereturn.
In AP CSP pseudocode
- ข้อสอบใช้ pseudocode สำหรับ procedures
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.
ข้อผิดพลาดที่พบบ่อย
- A procedure with a
return/value gives back a value; call it with() - Abstraction hides the detail behind a simple name
Now you try
- Read each task to see the procedure name and what it must return.
- Press Check answer to test your code.
ลองดูเลย
- อ่านแต่ละโจทย์เพื่อดูชื่อขั้นตอนการทำงานและสิ่งที่ต้องส่งกลับ
- กด Check answer เพื่อทดสอบโค้ดของคุณ
A procedure call · การเรียก Procedure
Calling a procedure passes arguments in and returns a value out. · การเรียก Procedure ส่งอาร์กิวเมนต์เข้า และ คืนค่าออกมา
Write a procedure square(n) that returns n multiplied by itself. Do not print inside it — use return. · เขียน procedure square(n) ที่ คืนค่า n คูณกับตัวเอง ไม่ต้องพิมพ์ข้างใน — ใช้ return
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Write a procedure area(width, height) with two parameters that returns the area (width times height). · เขียน procedure area(width, height) ที่มี สอง parameters ที่ คืนค่า พื้นที่ (กว้าง x สูง)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The procedure double(x) is given. Call it with 21 and store the result in a variable named result. · procedure double(x) ถูกกำหนดให้ Call ด้วย 21 และเก็บผลลัพธ์ในตัวแปรชื่อ result
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่