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. · اضغط تشغيل لرؤية المخرجات هنا.