fun (function)
Try it yourself →Defines your own command.
Syntax
fun name(p: Type): ReturnType { return ... }Description
Defined with fun; the return type is written with a colon (:). Single-expression functions can be written with an equals sign (=): fun square(x: Int) = x * x.
Example
fun square(x: Int): Int {
return x * x
}
println(square(5))Expected output
25
When to use?
Preventing code repetition, breaking the program into parts.