الوسيطات الموضعية والمسمّاة
مرّر القيم حسب الموضع أو الاسم
الوسيطات الموضعية والمسمّاة درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Mojo Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Mojo Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Ways to Pass Values
Every Mojo function call sends in values called arguments. You can send them by position or by name, and both styles can live in one call.
Positional Arguments
With positional arguments, order is everything. The first value fills the first parameter, the second fills the second, and so on.
fn greet(name: String, times: Int):
for _ in range(times):
print(name)
greet("Mojo", 2)Order Matters Here
Swap two positional arguments and the meaning changes. Here Mojo would read 2 as the name and "Mojo" as the count, so order must match the parameters.
greet(2, "Mojo") # wrong: positions no longer matchKeyword Arguments
A keyword argument names the parameter you mean. Now the call reads clearly and order stops mattering.
greet(name="Mojo", times=2)Keywords Free You from Order
Because each value is labeled, you can list keyword arguments in any order. Mojo matches them by name, not by position.
greet(times=2, name="Mojo") # same resultMixing Both Styles
You can combine the two, but positional values must come first. Once a keyword appears, everything after it must also be a keyword.
greet("Mojo", times=2) # ok
# greet(name="Mojo", 2) # errorWhy Keywords Help Readers
Keyword arguments turn a mystery call into a sentence. Reading times=2 tells you exactly what the 2 does, even months later.
Avoiding Position Bugs
When a function takes several same-typed values, positions are easy to mix up. Naming them with keywords removes that whole class of bug.
draw(width=800, height=600) # clearer than draw(800, 600)Names Come from Parameters
The keyword you type must match the parameter name in the function definition. A typo like nme= is an error, not a new argument.
fn greet(name: String, times: Int): ...
greet(name="Mojo", times=2)Both Styles, One Function
You do not pick a style per function. The caller decides, so the same function accepts positional, keyword, or a mix on each call.
A Practical Rule of Thumb
Use positional for the obvious leading value, then switch to keywords for extras. This keeps short calls tidy and long calls clear.
connect("localhost", port=8080, retries=3)Quick Check
You want order to stop mattering in a function call. Which approach helps?
Recap: By Position or Name
Positional arguments rely on order; keyword arguments rely on names. Lead with positions, label the rest, and your calls stay both short and clear.
الأسئلة الشائعة
هل درس «الوسيطات الموضعية والمسمّاة» مجاني؟
نعم — نص درس «الوسيطات الموضعية والمسمّاة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «الوسيطات الموضعية والمسمّاة»؟
مرّر القيم حسب الموضع أو الاسم تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «الوسيطات الموضعية والمسمّاة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الوسيطات الموضعية والمسمّاة
- القيم الافتراضية للوسيطات
- إرجاع قيم متعددة
- قراءة توقيعات الدوال