0Pricing
Mojo Academy · บทเรียน

อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ

ส่งค่าตามตำแหน่งหรือตามชื่อ

อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 match

Keyword 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 result

Mixing 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)  # error

Why 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.

คำถามที่พบบ่อย

บทเรียน “อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ”

ส่งค่าตามตำแหน่งหรือตามชื่อ คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. อาร์กิวเมนต์ตามตำแหน่งและตามชื่อ
  2. ค่าเริ่มต้นของอาร์กิวเมนต์
  3. การส่งคืนหลายค่า
  4. การอ่านลายเซ็นฟังก์ชัน
← กลับไปที่ Mojo Academy