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

พารามิเตอร์เทียบกับอาร์กิวเมนต์

ค่าขณะคอมไพล์เทียบกับค่าขณะทำงาน

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

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

Two Kinds of Input

Mojo functions can take two kinds of input: parameters known at compile time, and arguments known at runtime. Knowing the difference is the key.

Arguments Live at Runtime

An argument is an ordinary value you pass when the program runs. Its actual number can change every single time you call the function.

fn square(x: Int) -> Int:
    return x * x

Parameters Live at Compile Time

A parameter is a value the compiler already knows before your program runs. It is fixed and baked right into the generated code. ⚙️

Brackets vs Parentheses

Mojo keeps them visually separate: parameters go in square brackets, while arguments stay in the familiar parentheses.

fn repeat[count: Int](msg: String):
    print(msg)

Reading the Signature

In repeat[count: Int](msg: String), the count is set at compile time and msg is supplied later when you actually call it.

Compile Time Means Earlier

Think of compile time as the moment Mojo translates your code, long before it runs. Parameters are decided in that earlier phase.

Why Separate Them

Keeping compile-time and runtime values separate lets the compiler specialize and optimize code that the runtime simply cannot.

Passing a Parameter

You supply a parameter in brackets at the call site, just like you supply arguments in parentheses right after it.

repeat[3]("hi")

Parameters Must Be Known

A parameter value must be a compile-time constant. You cannot pass a number you only computed while the program was running.

A Familiar Example

The built-in SIMD type takes its element type and width as parameters, then holds real numbers as runtime values inside it.

var v = SIMD[DType.int32, 4](1, 2, 3, 4)

The Mental Model

Picture two slots: the bracket slot for compile-time facts, the parenthesis slot for runtime data. Mojo treats them very differently.

Quick Check

Tell apart parameters and arguments in Mojo.

Recap

You learned the split: parameters are compile-time values in brackets, arguments are runtime values in parentheses. That divide powers Mojo's speed. 🎯

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

บทเรียน “พารามิเตอร์เทียบกับอาร์กิวเมนต์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พารามิเตอร์เทียบกับอาร์กิวเมนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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. Struct ที่รับพารามิเตอร์
  4. เหตุใดการคำนวณขณะคอมไพล์จึงเพิ่มความเร็ว
← กลับไปที่ Mojo Academy