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

การเขียนฟังก์ชัน fn

เคร่งครัด มีชนิดข้อมูล และเน้นประสิทธิภาพ

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

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

Meet the fn Keyword

The fn keyword defines a stricter function. It trades some flexibility for safety and the speed Mojo is famous for. ⚡

fn greet():
    print("Hello")

Types Are Required

Every parameter in an fn must declare its type. This lets the compiler check your code and generate fast machine code.

fn add(a: Int, b: Int) -> Int:
    return a + b

Declare the Return Type

An fn states what it returns with an arrow, like -> Int. If it returns something, the type must be spelled out.

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

No Return Type Means None

Leave off the arrow when an fn returns nothing. It performs an action, like printing, and hands back no value.

fn shout(msg: String):
    print(msg)

Arguments Are Immutable by Default

In an fn, arguments are read-only by default. Trying to reassign one is a compile error, which prevents subtle bugs.

fn show(x: Int):
    print(x)

The Compiler Has Your Back

Because types are known, the compiler catches mismatches before the program runs, turning many runtime crashes into early errors.

Strictness Unlocks Speed

Knowing every type ahead of time lets Mojo skip dynamic checks, so an fn can reach the performance of low-level languages.

Local Variables Need var

Inside an fn, declare new local variables with var. The strict scope rules keep your function predictable.

fn total() -> Int:
    var sum = 0
    sum = sum + 5
    return sum

Errors Must Be Declared

An fn that can fail must say so with raises. Without it, the compiler assumes the function never throws.

fn risky() raises:
    raise Error("nope")

Same Indentation Rules

Like def, an fn uses indentation for its body. The difference is the strictness of types, not the surrounding layout.

fn info():
    print("a")
    print("b")

Built for Hot Paths

Reach for fn in the parts of your program that run millions of times, where every saved nanosecond truly adds up.

Quick Check

Recall the default behavior of fn arguments.

Recap

You met fn: strict, typed, and fast. Types are required, arguments are read-only by default, and the compiler guards correctness. 🎯

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

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

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

คุณจะเรียนรู้อะไรในบทเรียน “การเขียนฟังก์ชัน fn”

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

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

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

บทเรียน “การเขียนฟังก์ชัน fn” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การเขียนฟังก์ชัน def
  2. การเขียนฟังก์ชัน fn
  3. ควรเลือกใช้แบบใด
  4. ชนิดค่าที่ส่งคืนและ None
← กลับไปที่ Mojo Academy