การส่งคืนหลายค่า
ส่งทูเพิลกลับจากฟังก์ชัน
การส่งคืนหลายค่า เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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) # 3Names 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]) # 3Why 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 successTuples 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.
คำถามที่พบบ่อย
บทเรียน “การส่งคืนหลายค่า” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การส่งคืนหลายค่า” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การส่งคืนหลายค่า”
ส่งทูเพิลกลับจากฟังก์ชัน คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การส่งคืนหลายค่า” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม
ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ