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 تمرر مرجعًا إلى العنصر الذي تعطيه إياه.
- الأرقام والنصوص ثابتة (immutable): لا يمكن للدالة تغيير قيمة المستدعي.
- القوائم متغيرة (mutable): التغييرات التي تحدث داخل الدالة تؤثر حقًا على قائمة المستدعي.
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.
في pseudocode كامبريدج (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. · اضغط تشغيل لرؤية المخرجات هنا.