ฟังก์ชันทักทายใน commonMain
เขียนฟังก์ชัน Kotlin ข้ามแพลตฟอร์มแรกของคุณ
ฟังก์ชันทักทายใน commonMain เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Meet commonMain
Code you put in commonMain is shared by every platform. It is the heart of KMP, so this is where your first function will live.
Pure Kotlin Only
Inside commonMain you write pure Kotlin with no Android or iOS imports. If it compiles here, it runs the same on both apps.
Pick a Tiny Goal
Your first shared piece is a small greeting function. It takes a name and returns a friendly line that both apps can show. 👋
Write the Function
Declare it with fun and give it one parameter. This single line of Kotlin is now available to Android and iOS alike.
fun greet(name: String): String {
return "Hello, " + name + "!"
}String Templates
Kotlin string templates read cleaner than joining with plus. The dollar sign drops a value straight into the text.
fun greet(name: String): String {
return "Hello, $name!"
}Return Types Matter
The : String after the parentheses promises a String comes back. Both platforms rely on that contract being honored.
Keep It Side-Effect Free
A good shared function is pure: same input, same output, no logging or UI. That is exactly what makes it safe everywhere.
Default Arguments
Kotlin lets you set a default value, so callers can skip the argument. Both apps then get a sensible greeting for free.
fun greet(name: String = "there"): String {
return "Hello, $name!"
}Where the File Lives
Save it under commonMain/kotlin in your shared module. The folder is the signal to KMP that this code targets every platform.
Top-Level Functions
You did not need a class. A top-level function in a file is perfectly fine and keeps tiny shared utilities simple.
One Source of Truth
This single greet function is the one source of truth. Change the wording once and every platform speaks the same way. ✨
Quick Check
Let us check where shared code belongs.
Recap
You wrote your first shared function in commonMain: pure Kotlin, a clear return type, and one source of truth for both apps. 🎉
คำถามที่พบบ่อย
บทเรียน “ฟังก์ชันทักทายใน commonMain” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ฟังก์ชันทักทายใน commonMain” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชันทักทายใน commonMain”
เขียนฟังก์ชัน Kotlin ข้ามแพลตฟอร์มแรกของคุณ คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “ฟังก์ชันทักทายใน commonMain” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม
ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ฟังก์ชันทักทายใน commonMain
- เรียกใช้โค้ด shared จากแอป Android
- เรียกใช้โค้ด shared จากแอป iOS
- เปลี่ยนครั้งเดียว เห็นผลในทั้งสองแอป