Functions

Functions

Function Declarations

Functions in Kotlin are declared using the fun keyword

fun double(x: Int): Int {
}

Function Usage

Calling functions uses the traditional approach

val result = double(2)

Calling member functions uses the dot notation

Sample().foo() // create instance of class Sample and calls foo

Infix notation

Functions can also be called using infix notations when

  • They are member functions or extension functions
  • They have a single parameter
  • They are marked with the infix keyword
// Define extension to Int
infix fun Int.shl(x: Int): Int {
...
}

// call extension