AnyCompiler

Functions

A function is declared with fun, a name, parameters in parentheses, and an optional return type after a colon. When a function's body is a single expression, you can skip the braces and return keyword entirely and just write = expression — the return type is then inferred automatically.

fun multiply(a: Int, b: Int): Int {
    return a * b
}

fun square(x: Int) = x * x

fun main() {
    println(multiply(6, 7))
    println(square(5))
}
Output
42
25

Next: null safety — the feature Kotlin is best known for, and what a real null-safety compile error looks like.

Practice exercise

Write a function multiply(a: Int, b: Int) that returns a * b, then print multiply(6, 7).