A _function literal_ as an "anonymous function", i.e. a function that is not declared, but passed immediately as an expression. Consider the following example:
{jet}
max(strings, {a, b -> a.length < b.length})
{jet}
Function {{max}} is a [higher-order function|Functions#Higher-order functions], i.e. is takes a function value as the second argument. This second argument is an expression that is itself a function, i.e. a _function literal_. As a function, it is equivalent to
{jet}
fun compare(a : String, b : String) : Boolean = a.length < b.length
{jet}

h3. Function types

For a function to accept another function as a parameter, we have to specify a _function type_ for that parameter. For example the abovementioned function {{max}} is defined as follows:
{jet:lineNumbers=true}
fun max<T>(collection : Collection<out T>, less : (T, T) -> Boolean) : T? {
  var max : T? = null
  for (it in collection)
    if (max == null || less(max, it))
      max = it
  return max
}
{jet}
The parameter {{less}} is of type {{(T, T) -> Boolean}}, i.e. a function that takes two parameters of type {{T}} and returns a {{Boolean}}: *true* if the first one is smaller than the second one.

In the body, line 4, {{less}} is used as a function: it is called by passing two arguments of type {{T}}.

A _function type_ is written as above, or may have named parameters, for documentation purposes and to enable calls with [named arguments|Functions#Named arguments].
{jet}
val compare : (x : T, y : T) -> Int = ...
{jet}

{anchor:Syntax}

h3. Syntactic forms of function literals

The _full_ syntactic form of function literals, i.e. literals of function types, is as follows:
{jet}
val sum = {(x : Int, y : Int) : Int -> x + y}
{jet}

* A function literal is always surrounded by curly braces,
* parameter declarations in the full syntactic form go inside parentheses and have _optional_ type annotations,
* the _optional_ return type annotation goes after the parameter list,
* the body goes after an '{{->}}' sign.

If we leave all the optional annotations out, what's left looks like this:
{jet}
val sum : (Int, Int) -> Int = {(x, y) -> x + y}
{jet}
As this is the most common case, [Kotlin] allows us to leave the parentheses out as well, if no type annotations are present, and so we get the _short_ syntactic form for functional literals:
{jet}
val sum : (Int, Int) -> Int = {x, y -> x + y}
{jet}

It very common that a function literal has _only one parameter_. If [Kotlin] can figure the signature out itself, it allows us not to declare the only parameter, and will implicitly declare it for us under the name *it*:
{jet}
ints.filter {it > 0} // this literal is of type '(it : Int) -> Boolean'
{jet}
Note that if a function takes another function as the last parameter, the function literal argument can be passed outside the parenthesized argument list. See [Higher-order functions|Functions#Higher-order functions] and the grammar for [callSuffix|Grammar#callSuffix].

See the grammar for function literals [here|Grammar#functionLiteral].

h3. Closures

A function literal (as well as a [local function|Functions#Local functions] and [object expressions|Object expressions and Declarations]) can access its _closure_, i.e. the variables declared in the outer scope. Unlike *Java* the closure variables can be modified:
{jet}
var sum = 0
ints filter {it > 0} forEach {
  sum += it
}
print(sum)
{jet}

{anchor:Extensions}

h3. Extension function literals

Besides ordinary functions, [Kotlin] supports [extension functions|Extension functions]. This kind of functions in so useful, that extension function literals are also supported. One of the most important examples of their usage is [Type-safe Groovy-style builders|Type-safe Groovy-style builders].

An extension function differs from an ordinary one in that it has a _receiver type_ specification. One can specify a receiver type in a function literal as well:
{jet}
val sum = {Int.(other : Int) : Int -> this + other}
{jet}
Receiver type may be specified only in the _full_ syntactic form of a function literal (remember that parameter types and return type annotations are optional in this form).

Such a literal has a function type with receiver:
{jet}
sum : Int.(other : Int) -> Int
{jet}
it can be called with a dot or in infix form (since it has only one parameter):
{jet}
1.sum(2)
1 sum 2
{jet}

h4. Disambiguation of {{this}} expressions

See [*This* expressions|This expressions#Qualified].

h3. What's next

*Expressions*
* [Returns and jumps]
* [Ranges]
* [This expressions]
* [Tuples]
* [Type casts]

*Functions*
* [Type-safe Groovy-style builders]