0Pricing
Mojo Academy · درس

إرجاع قيم متعددة

أعد tuples من دالة

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

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

More Than One Result

Sometimes a function naturally produces two answers at once. In Mojo you can hand them all back together using a tuple.

Returning a Tuple

List the values separated by commas in the return. Mojo packs them into one tuple that the caller can take apart.

fn min_max(a: Int, b: Int) -> (Int, Int):
    if a < b:
        return (a, b)
    return (b, a)

Declaring the Tuple Type

The arrow type spells out each slot. Here (Int, Int) tells readers and the compiler exactly what comes back.

fn split() -> (String, Int): ...

Unpacking the Result

The caller can unpack the tuple into separate names in one line, giving each value a clear identity right away.

lo, hi = min_max(8, 3)
print(lo)  # 3

Names Match Order

Unpacking follows position. The first name takes the first value, the second name the second, so the order must line up with the return.

low, high = min_max(8, 3)

Or Keep the Whole Tuple

You do not have to unpack. Hold the tuple in one variable and reach into it by index when you need a piece.

pair = min_max(8, 3)
print(pair[0])  # 3

Why Not Just One Return?

Bundling related results keeps them together. Returning both a value and its status in one tuple is cleaner than scattering them across globals.

fn parse() -> (Int, Bool): ...  # value and success

Tuples Can Mix Types

A tuple is not limited to one type. You can return a String and an Int side by side, each keeping its own type.

fn user() -> (String, Int):
    return ("Ada", 36)

Reading Multi-Value Returns

When you see an arrow leading to parentheses, expect several results. The signature is your map for how to unpack them.

Keep Tuples Small and Clear

Two or three values in a tuple read well. If you need many, a named struct usually communicates intent better than a long tuple.

A Common Pattern

Returning a result plus an error or flag is a classic use of tuples. The caller unpacks both and reacts to each in turn.

value, ok = try_parse(text)

Quick Check

How does a Mojo function hand back more than one value at once?

Recap: Bundle and Unpack

Return several results as a tuple, declare its type in the signature, then unpack by position. Keep tuples small or reach for a struct.

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

هل درس «إرجاع قيم متعددة» مجاني؟

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

ماذا ستتعلم في «إرجاع قيم متعددة»؟

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

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

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

كم من الوقت يستغرق درس «إرجاع قيم متعددة»؟

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

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

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

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

  1. الوسيطات الموضعية والمسمّاة
  2. القيم الافتراضية للوسيطات
  3. إرجاع قيم متعددة
  4. قراءة توقيعات الدوال
← العودة إلى Mojo Academy