Completed Coursera: Kotlin for developers course

This commit is contained in:
Martin Berg Alstad
2025-07-30 14:34:03 +02:00
commit 80c14c76ba
41 changed files with 608 additions and 0 deletions

34
src/Functions.kt Normal file
View File

@ -0,0 +1,34 @@
// Specify the name to use when calling from another language
@file:JvmName("fun")
fun main() {
print(max(2, 3))
println(
// Named arguments are optional
listOf("a", "b", "c").joinToString(
separator = "",
prefix = "(",
postfix = ")"
)
)
// Using default value
println(repeatString("*"))
println(repeatString("*", 2))
// Flip the arguments by using named arguments
println(repeatString(times = 5, s = "*"))
}
// Top-level function
fun max(a: Int, b: Int): Int = if (a > b) a else b
// Unit return type
fun print(value: Int) = println("Max is $value")
// Times has a default value
fun repeatString(s: String, times: Int = 10): String = s.repeat(times)
// Generates 4 overloaded functions for the JVM. Not needed when only using Kotlin
@JvmOverloads
fun sum(a: Int = 0, b: Int = 0, c: Int = 0) = a + b + c