A Greeting Function in commonMain
Write your first cross-platform Kotlin function.
A Greeting Function in commonMain is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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. 🎉
Frequently asked questions
Is the “A Greeting Function in commonMain” lesson free?
Yes — the full text of “A Greeting Function in commonMain” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “A Greeting Function in commonMain”?
Write your first cross-platform Kotlin function. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “A Greeting Function in commonMain” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Greeting Function in commonMain
- Call Shared Code from the Android App
- Call Shared Code from the iOS App
- Change Once, See It on Both Apps