פונקציות, מסגרות קריאה ותוצאות הוחזרות
הוצג| English | משמעות |
|---|---|
| function/ˈfʌŋkʃn/ | פונקציה |
| side effect/saɪd ɪˈfekt/ | אפקט לוואי |
| contract/ˈkɒntrækt/ | חוזה |
| parameter/pəˈræmɪtə/ | פרמטר פורמלי |
| argument/ˈɑːɡjuːmənt/ | פרמטר אקטואלי |
| call frame/kɔːl freɪm/ | מסגרת קריאה |
| scope/skəʊp/ | תחום תוקף |
| local variable/ˈləʊkl ˈveərɪəbl/ | משתנה מקומי |
| decomposition/ˌdiːkɒmpəˈzɪʃn/ | פירוק |
Give each function one clear job
- A function 函数 groups a named task. State its inputs, its returned result and any side effect 副作用, such as printing, before writing the body.
- A contract 契约 says what callers may supply and what the function promises. Here, amounts are whole pence and must be non-negative integers.
def add_fee(amount):
"""Return amount plus a fixed fee of 3 pence."""
return amount + 3
add_fee(8) evaluates to 11. It does not print anything. Keeping calculation separate from display lets another function reuse the result.
אילו חוזה מאפשר לחישוב אחר להשתמש בסכום הכולל עמלה ישירות?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
תוצאה מוחזרת יכולה לשמש ביטוי; הדפסה היא אפקט לוואי נפרד.
Distinguish the input name from the input object
- In a definition,
amountis a parameter 形参. Inadd_fee(8), the expression8supplies an argument 实参. Argument expressions are evaluated before the function body runs. - The call binds the parameter name to the supplied object. It does not automatically copy that object or create a link between the caller's name and the parameter's name.
amount = 4
result = add_fee(amount * 2)
print(amount, result)
The argument expression gives 8; inside the call, amount refers to that integer. The returned 11 becomes result. The module's amount still refers to 4, so the output is 4 11.
התאם כל מונח לתפקידו ב-add_fee(8).
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
הארגומנט מוערך בקריאה; האובייקט שלו קשור לפרמטר; ההחזרה מעבירה תוצאה חזרה.
Keep a separate frame for each call
- A call frame 调用帧 records one active function call, including its parameters and local names. When one function calls another, the caller waits for the returned value.
- Read this program in execution order: module →
twice→add_fee→twice→ module. The arrows below are conceptual control flow, not a physical memory layout.
def twice(amount):
subtotal = amount * 2
return add_fee(subtotal)
amount = 4
result = twice(amount)
print(amount, result)

Use the trace to inspect the calls and returns. The frame details show the state at the nested call. It is an authored trace of this exact example, not a Python interpreter.
עקוב אחרי twice(4) המכנה add_fee(8)
הפרד בין קישורים מקומיים לבין ערכים מוחזרים.
סדר את אירועים אלו לפי הסדר שבו נעשה שימוש ב- result = twice(4).
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
הקריאה המקווננת מסתיימת לפני ש- twice יכול להשלים את ביטוי ההחזרה שלה.
איזה מספר שלם קשור ל- amount בתוך add_fee במהלך הקריאה המקווננת הזו?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
twice מחשבת סכום ביניים כ- 4 כפול 2, ולאחר מכן עוברת את 8 הזה ל- add_fee.
Return a result; print a message
returnends the current call and gives an object back to the caller. A function that reaches its end withoutreturngivesNone.printwrites output. It also returnsNone, so printing a calculation inside a function does not make that calculation its returned result.
def show_fee(amount):
print(amount + 3)
value = show_fee(8)
print(value)
This prints 11, then None, on separate lines. If the caller needs the number, use return amount + 3 and let the caller decide whether to print it.
show_fee מדפיסה 11 אך אין לה פקודת החזרה. מה מאוחסן ב- value?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
יציאה מהסוף של פונקציה Python מחזירה None, ללא קשר לתוכן שהודפס.
Resolve names in the right scope
- A local variable 局部变量 belongs to a function call. Its scope 作用域 determines where a name can be resolved; another function does not receive access to the caller's local names automatically.
- Assigning a parameter a new integer changes its local binding. It does not reassign a same-named variable in the caller.
def increase(amount):
amount = amount + 1
return amount
amount = 4
new_amount = increase(amount)
print(amount, new_amount)
The output is 4 5. Both names begin by referring to the integer 4; the assignment inside the function rebinds its local name. To update the module name, write amount = increase(amount).
לאחר new_amount = increase(amount), איזה ערך עדיין מוחזק ב- amount של המודול?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
רק השם המקומי של הפונקציה הושב מחדש; הקורא לא ביצע השמה ל- amount.
Decompose a calculation into testable pieces
- Decomposition 分解 separates a larger task into smaller jobs with clear contracts. For an order, calculate an item subtotal, add a fee, then display the final amount.
- Test calculations directly, including zero quantity and more than one item. State that inputs are non-negative integers; input validation can be a separate function.
def item_subtotal(unit_pence, quantity):
return unit_pence * quantity
def order_total(unit_pence, quantity):
return add_fee(item_subtotal(unit_pence, quantity))
order_total(5, 2) gives 13; order_total(5, 0) gives 3 under this fixed-fee contract. If zero-item orders should cost zero, change the contract and implementation together.
תחת הסכם העמלה הקבוע הנ"ל, מה מחזירה order_total(5, 0) בפנסים?
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
סכום הביניים של הפריט הוא אפס, אך add_fee עדיין מוסיפה 3. הזמנות ריקות חינמיות דורשות סכם אחר.
Explain the whole call before trusting the answer
- Trace the argument expressions, parameter bindings, local calculations and returned values. Check the caller's state after the call and the output separately.
- Do not describe parameter passing as copying every object. A mutable collection can be changed through a parameter; object diagrams for mutation and aliasing follow in unit 1.5.
A correct printed number does not prove a correct returned result. Use direct checks such as assert order_total(5, 2) == 13, then inspect any expected output separately.
Language references: Python functions and name binding and scope.
קריאה לפונקציה Python מעתיקה אוטומטית כל אובייקט ארגומנט.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
פרמטרים קשורים לאובייקטים המסופקים. העתקה יש לבקש במפורש כאשר נדרשת.