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

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

รูปแบบ Python ยืดหยุ่นและไม่เคร่งครัด

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

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

Functions Bundle Logic

A function packages a piece of work under a name so you can run it whenever you like, instead of repeating code. 🧩

Meet the def Keyword

The def keyword defines a function the Python way: relaxed about types and easy to write while you explore an idea.

def greet():
    print("Hello from Mojo")

Calling Your Function

Defining a function does not run it. You call it by writing its name followed by parentheses, and the body executes.

greet()

Passing in Arguments

Put parameters in the parentheses to feed data in. Here name becomes a value you can use inside the body.

def greet(name):
    print("Hi", name)

Types Are Optional in def

In a def you may skip type annotations entirely. Mojo lets the value flow dynamically, just like Python does.

def double(x):
    return x * 2

Returning a Value

The return keyword hands a result back to whoever called the function, ending the function right there.

def add(a, b):
    return a + b

Arguments Are Mutable Copies

Inside a def, arguments act like local copies you can reassign freely without touching the caller value.

def bump(x):
    x = x + 1
    return x

def Feels Like Python

If you know Python, def is instantly familiar: same syntax, same forgiving feel, now running on Mojo.

Great for Quick Experiments

Because it asks for so little, def shines when prototyping or scripting, where speed of writing matters most.

Indentation Defines the Body

Everything indented under the def line is the function body. Mojo, like Python, uses indentation to group code.

def info():
    print("line one")
    print("line two")

Docstrings Document Intent

A string on the first body line is a docstring: a short note describing what the function does for future readers.

def area(r):
    "Return circle area."
    return 3.14 * r * r

Quick Check

Think about how def handles type annotations.

Recap

You met def: the Python-style function in Mojo. Types are optional, arguments are mutable copies, and it is perfect for quick, forgiving code. 🎯

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

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

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

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

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

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

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

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

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

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

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

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

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