0Pricing
Mojo Academy · درس

كتابة دالة fn

صارمة ومحددة الأنواع ومركّزة على الأداء

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

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

Meet the fn Keyword

The fn keyword defines a stricter function. It trades some flexibility for safety and the speed Mojo is famous for. ⚡

fn greet():
    print("Hello")

Types Are Required

Every parameter in an fn must declare its type. This lets the compiler check your code and generate fast machine code.

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

Declare the Return Type

An fn states what it returns with an arrow, like -> Int. If it returns something, the type must be spelled out.

fn square(x: Int) -> Int:
    return x * x

No Return Type Means None

Leave off the arrow when an fn returns nothing. It performs an action, like printing, and hands back no value.

fn shout(msg: String):
    print(msg)

Arguments Are Immutable by Default

In an fn, arguments are read-only by default. Trying to reassign one is a compile error, which prevents subtle bugs.

fn show(x: Int):
    print(x)

The Compiler Has Your Back

Because types are known, the compiler catches mismatches before the program runs, turning many runtime crashes into early errors.

Strictness Unlocks Speed

Knowing every type ahead of time lets Mojo skip dynamic checks, so an fn can reach the performance of low-level languages.

Local Variables Need var

Inside an fn, declare new local variables with var. The strict scope rules keep your function predictable.

fn total() -> Int:
    var sum = 0
    sum = sum + 5
    return sum

Errors Must Be Declared

An fn that can fail must say so with raises. Without it, the compiler assumes the function never throws.

fn risky() raises:
    raise Error("nope")

Same Indentation Rules

Like def, an fn uses indentation for its body. The difference is the strictness of types, not the surrounding layout.

fn info():
    print("a")
    print("b")

Built for Hot Paths

Reach for fn in the parts of your program that run millions of times, where every saved nanosecond truly adds up.

Quick Check

Recall the default behavior of fn arguments.

Recap

You met fn: strict, typed, and fast. Types are required, arguments are read-only by default, and the compiler guards correctness. 🎯

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

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

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

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

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

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

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

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

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

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

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

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

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