Functions · الدوال
Name a block of code
- A function is named code you can use again and again.
- Make one with
def name():and an indented body. - "Calling" the function by its name runs that code.
تسمية كتلة من الكود
- الدالة هي كود مسمى يمكنك استخدامه مرارًا وتكرارًا.
- أنشئ واحدة باستخدام
def name():وجسم مسند (indented body). - "استدعاء" الدالة باسمها يشغل ذلك الكود.
Give it inputs (parameters)
- The names in the brackets are parameters — the function's inputs.
- When you call it, you pass in arguments (the real values).
- A function can take more than one parameter.
إعطاؤها مدخلات (معاملات)
- الأسماء داخل الأقواس هي المعاملات — وهي مدخلات الدالة.
- عند استدائها، تمرر الحجج (القيم الفعلية).
- يمكن للدالة أن تأخذ أكثر من معامل واحد.
def shout(word):
print(word.upper() + "!")
shout("hello")
Send back a result (return)
returnsends a value back to whoever called the function.- You can store that value or use it in an expression.
returnalso ends the function straight away.
إرسال نتيجة للخلف (إرجاع)
returnيرسل قيمة للخلف إلى من استدعى الدالة.- يمكنك تخزين تلك القيمة أو استخدامها في تعبير.
returnينهي الدالة أيضًا فورًا.
def double(x):
return x * 2
result = double(5)
print(result)
return vs print
printonly shows something on the screen.returngives a value back that the rest of your code can use.- A function with no
returngives backNone.
return مقابل print
printيعرض شيئًا فقط على الشاشة.returnيرجع قيمة يمكن لباقي كودك استخدامها.- الدالة التي لا تحتوي على
returnترجعNone.
In Cambridge pseudocode
- A
FUNCTIONreturns a value; aPROCEDUREjust does an action.
في الرمز الوهمي لكامبريدج
- الـ
FUNCTIONيُرجع قيمة؛ أما الـPROCEDUREفيقوم بإجراء فقط.
FUNCTION Add(A : INTEGER, B : INTEGER) RETURNS INTEGER
RETURN A + B
ENDFUNCTION
PROCEDURE Greet(Name : STRING)
OUTPUT "Hello, ", Name
ENDPROCEDURE
Common mistakes
- A function returns
Noneunless you writereturn; printing is not the same as returning. - Run a function with
():greet(), notgreet. - Define a function before you call it.
أخطاء شائعة
- الدالة تُرجع
Noneما لم تكتبreturn؛ الطباعة ليست نفس الإرجاع. - شغّل دالة باستخدام
():greet()، وليسgreet. - عرّف الدالة قبل استدائها.
Now you try
- Define each function, then the checks will call it for you.
- Use
returnto send the answer back (do notprintit).
الآن جرب بنفسك
- عرّف كل دالة، ثم ستقوم الاختبارات باستدائها لك.
- استخدم
returnلإرسال الإجابة للخلف (لا تقم بـprintلها).
Calling a function · استدعاء دالة
A call pushes a frame with its arguments; return pops it. · الاستدعاء يدفع إطارًا بحججه؛ ⌈⌊ return ⌋ سحبه.
Write a function add(a, b) that returns the sum of a and b. · اكتب دالة add(a, b) تُرجع مجموع a و b.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write a function greet(name) that returns the string Hello, then the name then !. So greet("Sam") returns Hello, Sam!. · اكتب دالة greet(name) تُرجع السلسلة النصية Hello, ثم الاسم ثم !. لذا فإن استدعاء greet("Sam") يُنتج Hello, Sam!.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write a function biggest(a, b) that returns the larger of the two values. If they are equal, return either one. · اكتب دالة biggest(a, b) تُرجع الأكبر بين القيمتين. إذا كانا متساويين، أرجع أيًا منهما.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write is_palindrome(word) that returns True when the word reads the same backwards (like racecar), otherwise False. · اكتب is_palindrome(word) ترجع True عندما يكون الكلمة مقلوبة بنفس الطريقة (مثل racecar)، وإلا False.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.