0Pricing
Mojo Academy · درس

متى تستخدم كل واحدة

طابق أسلوب الدالة مع هدفك

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

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

Two Tools, One Choice

Mojo gives you both def and fn. Picking well means matching the tool to what your code actually needs. 🧭

Choose def for Flexibility

Pick def when you are exploring, prototyping, or want Python-like ease without committing to types just yet.

def quick_test(data):
    return len(data)

Choose fn for Performance

Pick fn when speed and safety matter: tight loops, library code, and anywhere the compiler should verify your types.

fn fast_add(a: Int, b: Int) -> Int:
    return a + b

Prototype, Then Harden

A common path is to sketch logic with def, then rewrite the hot parts as fn once the design settles.

Type Safety Trade-off

With def you trade compile-time checks for freedom. With fn you trade freedom for early error detection.

Readability Counts Too

An fn signature reads like documentation: its types tell the next person exactly what goes in and what comes out.

fn distance(x: Float64, y: Float64) -> Float64:
    return x * x + y * y

Mix Both Freely

You can call an fn from a def and the other way around. They live happily together in the same Mojo file.

Interop Often Wants def

When juggling dynamic Python objects through interop, a def can be smoother, since types may not be known upfront.

Hot Loops Want fn

Numeric kernels and inner loops belong in fn, where known types let Mojo generate the tightest possible code.

There Is No Wrong Start

You can always begin with whichever feels natural. Switching a def to an fn later is a small, safe edit.

A Simple Rule of Thumb

If you would benefit from the compiler checking types and squeezing out speed, write fn; otherwise def keeps things light.

Quick Check

Match the situation to the better function style.

Recap

Reach for def when you want flexibility and quick iteration, and fn when you need type safety and speed. 🎯

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

هل درس «متى تستخدم كل واحدة» مجاني؟

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

ماذا ستتعلم في «متى تستخدم كل واحدة»؟

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

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

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

كم من الوقت يستغرق درس «متى تستخدم كل واحدة»؟

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

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

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

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

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