Skip to content

Instantly share code, notes, and snippets.

View sajjadyousefnia's full-sized avatar
🤒
Out sick

Sajjad Yousefnia sajjadyousefnia

🤒
Out sick
View GitHub Profile
fun printName(firstName: String, middleName: String = "N/A", lastName: String) {
println("first name: $firstName - middle name: $middleName - last name: $lastName")
}
printName("Chike", "Mgbemena") // won't compile
printName("Chike", lastName = "Mgbemena") // will now compile
@JvmOverloads
fun calCircumference(radius: Double, pi: Double = Math.PI): Double = (2 * pi) * radius
@JvmOverloads
fun calCircumference(radius: Double, pi: Double = Math.PI): Double = (2 * pi) * radius
// Java
double calCircumference(double radius, double pi);
double calCircumference(double radius);
fun printInts(vararg ints: Int): Unit {
for (n in ints) {
print("$n\t")
}
}
printInts(1, 2, 3, 4, 5, 6) // will print 1 2 3 4 5 6
fun printNumbers(myDouble: Double, myFloat: Float, vararg ints: Int) {
println(myDouble)
println(myFloat)
for (n in ints) {
print("$n\t")
}
}
printNumbers(1.34, 4.4F, 2, 3, 4, 5, 6) // will compile
fun printNumbers(myDouble: Double, vararg ints: Int, myFloat: Float) {
println(myDouble)
println(myFloat)
for (n in ints) {
print("$n\t")
}
}
printNumbers(1.34, 2, 3, 4, 5, 6, myFloat = 4.4F) // will compile
printNumbers(1.34, ints = 2, 3, 4, 5, 6, myFloat = 4.4F) // will not compile
printNumbers(myDouble = 1.34, ints = 2, 3, 4, 5, 6, myFloat = 4.4F) // will also not compile
val intsArray: IntArray = intArrayOf(1, 3, 4, 5)
printNumbers(1.34, intsArray, myFloat = 4.4F) // won't compile