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

ฟังก์ชันที่รับพารามิเตอร์

ทำให้ฟังก์ชันเฉพาะเจาะจงด้วยพารามิเตอร์ [วงเล็บ]

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

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

Functions With a Parameter

A parameterized function takes a compile-time value in square brackets, letting one definition adapt itself before the program runs. ⚙️

fn repeat[count: Int](msg: String):
    for i in range(count):
        print(msg)

The Bracket List

Place your parameters in brackets right after the function name, then your normal arguments in parentheses just after that.

fn scale[factor: Int](x: Int) -> Int:
    return x * factor

Calling With a Parameter

At the call site you fill the bracket with a constant, then pass runtime arguments in parentheses as usual.

var y = scale[10](4)

Each Value Makes a Version

Mojo builds a fresh specialized copy of the function for each parameter value you use, each one tuned exactly for that constant.

Type Parameters

A parameter can even be a type. This is how you write one function that works for Int, Float, or any type you choose.

fn first[T: AnyType](value: T) -> T:
    return value

Generic With Traits

Constrain a type parameter with a trait so the body can safely use the methods that trait promises every conforming type has.

fn show[T: Stringable](value: T):
    print(String(value))

The Compiler Inlines It

Because the parameter is a constant, the compiler can fold it directly into the code, often removing loops or branches entirely.

Mixing Parameters and Arguments

One function can take both: a compile-time parameter for structure and runtime arguments for the actual data it processes.

fn fill[n: Int](value: Int):
    for i in range(n):
        print(value)

Inferred Parameters

Sometimes Mojo can infer a parameter from the arguments, so you write less and the compiler fills in the rest for you.

Why It Stays Fast

Each specialization is its own optimized function with no runtime cost for the parameter, which is the heart of Mojo's speed.

One Source, Many Builds

You maintain a single readable definition, and the compiler quietly produces many fast, specialized builds behind the scenes.

Quick Check

Recall how a parameterized function is written.

Recap

You wrote parameterized functions: brackets hold compile-time values, the compiler specializes each version, and one source becomes many fast builds. 🎯

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

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

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