Una función de saludo en commonMain
Escriba su primera función Kotlin multiplataforma
Una función de saludo en commonMain es una lección gratuita de Kotlin Multiplatform Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Kotlin Multiplatform Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
Partes de esta lección aún no han sido traducidas y se muestran en inglés.
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. 🎉
Preguntas frecuentes
¿La lección «Una función de saludo en commonMain» es gratis?
Sí — el texto completo de «Una función de saludo en commonMain» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Kotlin Multiplatform Academy, actualiza a CoddyKit PRO. El curso de Kotlin Multiplatform Academy incluye 4 lecciones en total.
¿Qué aprenderé en «Una función de saludo en commonMain»?
Escriba su primera función Kotlin multiplataforma Practicas Kotlin Multiplatform Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.
¿Necesito experiencia previa para empezar Kotlin Multiplatform Academy?
No se requiere experiencia previa. Kotlin Multiplatform Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.
¿Cuánto tiempo toma la lección «Una función de saludo en commonMain»?
La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.
¿Puedo escribir y ejecutar código en esta lección de Kotlin Multiplatform Academy?
Sí. Cada lección de Kotlin Multiplatform Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.
Todas las lecciones de este curso
- Una función de saludo en commonMain
- Llame al código compartido desde la aplicación Android
- Llame al código compartido desde la aplicación iOS
- Cambie una vez y véalo en ambas aplicaciones