Scope & arguments · תחום חלון ופרמטרים
Local variables
- A variable made inside a function is local.
- It only exists while the function runs.
- Code outside the function cannot see it.
משתנים מקומיים
- משתנה שנצור בתוך פונקציה הוא מקומי.
- הוא קיים רק בזמן שהפונקציה רצה.
- קוד מחוץ לפונקציה לא יכול לראות אותו.
def make_message():
note = "secret"
print(note)
make_message()
# print(note) would be an error: note is local
Global variables
- A variable made outside all functions is global.
- A function can read a global value.
- To change a global inside a function you need the
globalkeyword — but it is usually cleaner toreturna value instead.
משתנים גלובליים
- משתנה שנצור חוץ מכל הפונקציות הוא גלובלי.
- פונקציה יכולה לקרוא ערך גלובלי.
- כדי לשנות ערך גלובלי בתוך פונקציה צריך את המילת המפתח
global— אך בדרך כלל נוח יותר ל-returnערך במקום זאת.
Default values
- A parameter can have a default value:
def f(x, step=1):. - If the caller leaves it out, the default is used.
- Parameters with defaults come after the normal ones.
ערכים ברירת מחדל
- פרמטר יכול לקבל ערך ברירת מחדל:
def f(x, step=1):. - אם הקורא משאיר אותו בחוץ, יושמש הערך ברירת המחדל.
- פרמטרים עם ערכי ברירת מחדל באים אחרי הרגילים.
def power(base, exp=2):
return base ** exp
print(power(5))
print(power(2, 3))
How arguments are passed
- Python passes a reference to the object you give it.
- Numbers and strings are immutable: a function cannot change the caller's value.
- Lists are mutable: changes made inside a function do affect the caller's list.
איך ארגומנטים עוברים
- Python מעבירה עזרה (רפרנס) לאובייקת שנתת לה.
- מספרים ומחרוזות הם בלתי משנים: פונקציה לא יכולה לשנות את הערך של הקורא.
- רשימות הן ניתנות לשינוי: שינויים שנעשו בתוך פונקציה אכן משפיעים על הרשימה של הקורא.
def try_change(n):
n = n + 100 # changes only the local copy
print(n)
x = 5
try_change(x)
print(x) # x is still 5
In Cambridge pseudocode (A-Level 9618)
BYVALpasses a copy (caller unchanged);BYREFshares the same data (caller changed).- ⚠️ These two keywords are A-Level 9618 only. IGCSE 0478 passes every parameter by value; to send a result back you use a
FUNCTIONandRETURNit.
בפסאודוקוד Cambridge (A-Level 9618)
BYVALמעביר העתק (הקורא אינו משתנה);BYREFמשתף את אותו נתון (הקורא משתנה).- ⚠️ שני מילות המפתח האלו הן רק ב-A-Level 9618. IGCSE 0478 מעבירה כל פרמטר בערך; כדי לשלוח תוצאה חזרה, משתמשים ב-
FUNCTIONו-RETURN.
PROCEDURE Increase(BYREF N : INTEGER)
N ← N + 1
ENDPROCEDURE
PROCEDURE Show(BYVAL N : INTEGER)
OUTPUT N
ENDPROCEDURE
Common mistakes
- A variable made inside a function is local — it cannot be seen outside.
- Arguments fill the parameters in the order you write them.
- To use a result outside,
returnit and store what comes back.
טעויות נפוצות
- משתנה שנוצרת בתוך פונקציה היא מקומית — היא לא נראית מחוץ לה.
- ארגומנטים ממלאים את הפרמטרים בסדר שכתבת אותם.
- כדי להשתמש בתוצאה מחוץ לפונקציה,
returnאותה ואחסן מה שמחזירות.
Now you try
- The checks call your function with different arguments.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- הבדיקות קוראות לפונקציה שלך עם ארגומנטים שונים.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Arguments live in the call · הפרמטרים קיימים בתוך הקריאה
Each call gets its own copy of the arguments — they vanish when it returns. · כל קריאה מקבלת עתיקה משלה של הפרמטרים — הם נעלמים כשהיא חוזרת.
Write greet(name, greeting="Hi") that returns greeting, then , , then name, then !. So greet("Sam") is Hi, Sam! and greet("Sam", "Hello") is Hello, Sam!. · כתוב greet(name, greeting="Hi") שתחזיר greeting, אחר כך , , אחר כך name, אחר כך !. לכן greet("Sam") הוא Hi, Sam! ו-greet("Sam", "Hello") הוא Hello, Sam!.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write add_item(lst, x) that appends x to the list lst. Do not return anything — the caller's list should change because lists are mutable. · כתוב פונקציה add_item(lst, x) שמוסיפה את הx לרשימה lst. אל תחזיר כלום — הרשימה של המכראי תשתנה כי רשימות הן משתנות.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write between(low, high, x) that returns True if x is from low to high (inclusive), otherwise False. · כתוב between(low, high, x) שתחזיר True אם x נמצא בין low ל-high (כולל הקצוות), אחרת False.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
Write make_countdown(start, stop=1) that returns a list counting down from start to stop. So make_countdown(3) is [3, 2, 1] and make_countdown(5, 3) is [5, 4, 3]. · כתוב make_countdown(start, stop=1) שתחזיר רשימה סופרת יורדת מ-start עד stop. לכן make_countdown(3) היא [3, 2, 1] ו-make_countdown(5, 3) היא [5, 4, 3].
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.