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.
מה נעשה
- פרוצדורה היא בלוק קוד בעל שם שאפשר להשתמש בו שוב ושוב.
- בפיתון, פרוצדורה נוצרת באמצעות
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.
פרמטרים מעבירים נתונים לתוך
- פרמטר הוא ערך שמקבלת הפרוצדורה.
- הכנס את שם הפרמטר בתוך ה-
(). - בתוך הפרוצדורה משתמשים בו כמו משתנה.
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). · כתוב תפקיד 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. · לחץ על הרץ כדי לראות את התוצא כאן.