0Pricing
Mojo Academy · درس

كتابة دالة def

بأسلوب Python، ديناميكية ومتسامحة

كتابة دالة def درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Functions Bundle Logic

A function packages a piece of work under a name so you can run it whenever you like, instead of repeating code. 🧩

Meet the def Keyword

The def keyword defines a function the Python way: relaxed about types and easy to write while you explore an idea.

def greet():
    print("Hello from Mojo")

Calling Your Function

Defining a function does not run it. You call it by writing its name followed by parentheses, and the body executes.

greet()

Passing in Arguments

Put parameters in the parentheses to feed data in. Here name becomes a value you can use inside the body.

def greet(name):
    print("Hi", name)

Types Are Optional in def

In a def you may skip type annotations entirely. Mojo lets the value flow dynamically, just like Python does.

def double(x):
    return x * 2

Returning a Value

The return keyword hands a result back to whoever called the function, ending the function right there.

def add(a, b):
    return a + b

Arguments Are Mutable Copies

Inside a def, arguments act like local copies you can reassign freely without touching the caller value.

def bump(x):
    x = x + 1
    return x

def Feels Like Python

If you know Python, def is instantly familiar: same syntax, same forgiving feel, now running on Mojo.

Great for Quick Experiments

Because it asks for so little, def shines when prototyping or scripting, where speed of writing matters most.

Indentation Defines the Body

Everything indented under the def line is the function body. Mojo, like Python, uses indentation to group code.

def info():
    print("line one")
    print("line two")

Docstrings Document Intent

A string on the first body line is a docstring: a short note describing what the function does for future readers.

def area(r):
    "Return circle area."
    return 3.14 * r * r

Quick Check

Think about how def handles type annotations.

Recap

You met def: the Python-style function in Mojo. Types are optional, arguments are mutable copies, and it is perfect for quick, forgiving code. 🎯

الأسئلة الشائعة

هل درس «كتابة دالة def» مجاني؟

نعم — نص درس «كتابة دالة def» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.

ماذا ستتعلم في «كتابة دالة def»؟

بأسلوب Python، ديناميكية ومتسامحة تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟

لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.

كم من الوقت يستغرق درس «كتابة دالة def»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟

نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. كتابة دالة def
  2. كتابة دالة fn
  3. متى تستخدم كل واحدة
  4. أنواع الإرجاع وNone
← العودة إلى Mojo Academy