🐦 Kotlin
Beginner
How do you declare a function in Kotlin?
Answer
Functions in Kotlin are declared using the fun keyword, followed by the function name, parameters, return type, and body. Example: fun add(a: Int, b: Int): Int { return a + b }. For single-expression functions, you can use the shorthand expression syntax: fun add(a: Int, b: Int): Int = a + b — the return type can even be inferred: fun add(a: Int, b: Int) = a + b. Functions that return nothing implicitly return Unit, which can be omitted from the signature.