الوظائف#

مقدمة#

تُعدّ الدوال عنصرًا أساسيًا في برمجة بايثون، حيث تُجمّع أجزاءً من التعليمات البرمجية في أقسام قابلة لإعادة الاستخدام وفعّالة، مصممة لأداء مهمة محددة. يمكن استدعاء الدوال عدة مرات داخل البرنامج، مما يُسهّل تنظيم التعليمات البرمجية ويُساعد على تجنّب تكرارها. كما تُسهّل الدوال عملية تصحيح الأخطاء.

هام: تعريف الدالة وحده لا يكفي لتشغيلها. استخدم vr_thread لبدء تشغيل الدالة في سلسلة عمليات جديدة، بحيث تبدأ بالعمل فور بدء المشروع، بدلاً من انتظار حدث أو رد نداء.

  • def يُعرّف دالة.

  • return يعيد مخرجات الدالة إلى البرنامج الرئيسي.

طريقة الاستخدام:

def function_name(parameters):
    # Code to execute when the function is called
    return result  # Optional, used to return a value

حدود

وصف

function_name

اسم تطلقه على وظيفتك.

parameters

اختياري. متغيرات تقبل قيم الإدخال عند استدعاء الدالة، مما يسمح بتمرير البيانات إلى الدالة.

result

اختياري. يسمح للدالة بإعادة النتيجة إلى المُستدعي. إذا لم تتضمن الدالة عبارة إرجاع، فستُرجع القيمة None افتراضيًا.

Note: A function must always be defined before it is called.

تعريف واستدعاء الدوال#

Functions with No Parameters#

إذا لم تتطلب الدالة إدخالاً، فيمكنك تعريفها بدون معلمات.

# Define a function to display a message
def greeting():
    brain.print("Hello!")

def main():
    # Call the function to display the message
    greeting()

# VR threads — Do not delete
vr_thread(main)

Functions with Parameters#

يمكنك أيضًا إضافة معلمات إلى الدوال، مما يسمح لك بتمرير المعلومات التي تحتاجها الدالة للعمل.

# Define a function with a parameter
def named_greeting(name):
    brain.print("Hello, " + name + "!")

def main():
    named_greeting("Stranger")

# VR threads — Do not delete
vr_thread(main)

Functions with Default Arguments#

الوسيط الافتراضي هو مُعامل يفترض قيمة افتراضية إذا لم يتم توفير قيمة في استدعاء الدالة لهذا الوسيط.

# Define a function with a parameter and a default argument
def named_greeting(name = "Stranger"):
    brain.print("Hello, " + name + "!")

def main():
    # Use the default argument
    named_greeting()
    brain.new_line()
    # Change the parameter to a different name
    named_greeting("VR")

# VR threads — Do not delete
vr_thread(main)

Return Values from Functions#

يمكن للدوال إرسال البيانات إلى المُستدعي باستخدام الكلمة المفتاحية return . وهذا يسمح لك بالتقاط المخرجات واستخدامها في برنامجك.

# Define a function that multiplies numbers by 2
def times_two(number):
    return number * 2

def main():
    # Display the return value
    brain.print(times_two(2))

# VR threads — Do not delete
vr_thread(main)

vr_thread#

يجب استخدام وظيفة VR Thread لاستدعاء الوظائف على المستوى العام للبرنامج النصي.

You can take any function you have made previously, and pass it to vr_thread() at the global level. Calling at least one function inside of this is required to run a program. You do not need to do all function calls with vr_thread(), just ones that are at the global level.

إذا تم إجراء أكثر من استدعاء واحد للدالة vr_thread() ، فسيتم تنفيذ الدوال بالتوازي.

تُنفَّذ الدوال، عمومًا، بالترتيب الذي تُستدعى به. مع ذلك، إذا انتظرت إحدى الدوال، فإنها تُمرِّر التنفيذ إلى الدالة التالية، وتستأنف التنفيذ عند انتهاء الدالة الأخرى، أو عندما تنتظر الدالة الأخرى أيضًا.

Usage:
vr_thread(function)

المعلمة

وصف

function

اسم دالة تم تعريفها مسبقاً.

# Define the function "main".
def main():
    drivetrain.drive_for(FORWARD, 400, MM)

# Call VR thread for any previously defined functions.
vr_thread(main)