الدوال ذات المعاملات
خصّص دالة باستخدام معاملات [بين قوسين]
الدوال ذات المعاملات درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Functions With a Parameter
A parameterized function takes a compile-time value in square brackets, letting one definition adapt itself before the program runs. ⚙️
fn repeat[count: Int](msg: String):
for i in range(count):
print(msg)The Bracket List
Place your parameters in brackets right after the function name, then your normal arguments in parentheses just after that.
fn scale[factor: Int](x: Int) -> Int:
return x * factorCalling With a Parameter
At the call site you fill the bracket with a constant, then pass runtime arguments in parentheses as usual.
var y = scale[10](4)Each Value Makes a Version
Mojo builds a fresh specialized copy of the function for each parameter value you use, each one tuned exactly for that constant.
Type Parameters
A parameter can even be a type. This is how you write one function that works for Int, Float, or any type you choose.
fn first[T: AnyType](value: T) -> T:
return valueGeneric With Traits
Constrain a type parameter with a trait so the body can safely use the methods that trait promises every conforming type has.
fn show[T: Stringable](value: T):
print(String(value))The Compiler Inlines It
Because the parameter is a constant, the compiler can fold it directly into the code, often removing loops or branches entirely.
Mixing Parameters and Arguments
One function can take both: a compile-time parameter for structure and runtime arguments for the actual data it processes.
fn fill[n: Int](value: Int):
for i in range(n):
print(value)Inferred Parameters
Sometimes Mojo can infer a parameter from the arguments, so you write less and the compiler fills in the rest for you.
Why It Stays Fast
Each specialization is its own optimized function with no runtime cost for the parameter, which is the heart of Mojo's speed.
One Source, Many Builds
You maintain a single readable definition, and the compiler quietly produces many fast, specialized builds behind the scenes.
Quick Check
Recall how a parameterized function is written.
Recap
You wrote parameterized functions: brackets hold compile-time values, the compiler specializes each version, and one source becomes many fast builds. 🎯
الأسئلة الشائعة
هل درس «الدوال ذات المعاملات» مجاني؟
نعم — نص درس «الدوال ذات المعاملات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «الدوال ذات المعاملات»؟
خصّص دالة باستخدام معاملات [بين قوسين] تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «الدوال ذات المعاملات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- المعاملات مقابل الوسيطات
- الدوال ذات المعاملات
- البنى ذات المعاملات
- لماذا تمنح الترجمة سرعة أكبر