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

ค่าเริ่มต้นของอาร์กิวเมนต์

กำหนดให้พารามิเตอร์บางตัวเป็นตัวเลือกได้

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Making a Parameter Optional

A default value lets a parameter stand in for itself. If the caller skips it, Mojo quietly uses the value you wrote in the definition.

fn greet(name: String, times: Int = 1):
    for _ in range(times):
        print(name)

Calling With and Without

Now both calls work. Omit times and you get the default of 1; pass it and you override the default for that one call.

greet("Mojo")        # times = 1
greet("Mojo", 3)     # times = 3

Why Defaults Are Handy

Defaults capture the common case. Most callers never touch the option, yet the power is there for the few who need it.

Defaults Go Last

Parameters with defaults must come after required ones. Otherwise Mojo could not tell which value you meant to skip.

fn open(path: String, mode: String = "r"): ...  # ok

A Required-Then-Optional Shape

Think of it as required-first, optional-last. Reading left to right, you supply what is needed, then accept defaults for the rest. This shape is predictable.

Overriding Just One Default

To change a later default while keeping earlier ones, use a keyword. You skip straight to the option you care about.

fn make(width: Int = 80, height: Int = 24): ...
make(height=40)  # width stays 80

Multiple Defaults Together

A function can give several parameters defaults at once. Callers fill only what they want and let the rest fall back.

fn box(w: Int = 10, h: Int = 10, fill: String = "x"): ...

Defaults Build Friendly APIs

Good defaults make a function easy to start with and powerful to grow into. Beginners call it plainly; experts tune the options.

Choosing a Sensible Default

Pick the value most callers would want anyway. A surprising default is worse than none, because it hides behavior the reader did not expect.

The Default Is Evaluated Once

The default expression belongs to the signature, not the body. Treat it as a fixed fallback that documents the normal value.

fn retry(count: Int = 3): ...  # 3 is the documented norm

Defaults Plus Keywords Shine

Defaults and keyword calls are a great pair. Together they let you write functions with many knobs that still feel simple to call.

request(url, timeout=30, retries=5)

Quick Check

Where must a parameter with a default value be placed in a Mojo function?

Recap: Smart Fallbacks

Default values make parameters optional and capture the common case. Put them last, choose them wisely, and override single ones with keywords.

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

บทเรียน “ค่าเริ่มต้นของอาร์กิวเมนต์” ฟรีหรือไม่

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

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

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

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

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

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

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

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

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

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

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