What is an anonymous function in Kotlin, and how does its syntax differ from a lambda expression?
answer
- fun keyword but no name
- explicit param + return types
- block body with normal return
- fun(x) = expr for expression body
- same function type as a lambda
basics
~20 sAn anonymous function is a function without a name, written with the fun keyword like fun(x: Int): Int { return x * 2 }. Unlike a lambda, you write parameter and return types explicitly and use a normal return.
solid answer
~50 sAn anonymous function is a function literal declared with the fun keyword but no name: fun(x: Int): Int { return x }. It is one of Kotlin's two function-literal forms (the other is the lambda, written with braces { }). The key syntactic differences: an anonymous function uses the fun keyword and parentheses for its parameter list and can declare its return type after the parameter list (e.g. : Int), whereas a lambda omits fun, puts parameters before -> inside braces, and infers the return type. Anonymous functions can have a block body with explicit return statements or an expression body (fun(x: Int) = x * 2). You assign one to a variable or pass it as an argument just like a lambda: val twice: (Int) -> Int = fun(x: Int) = x * 2.
code
kotlin · 7 linesval ints = listOf(1, 2, 3)
// lambda
val doubledL = ints.map { it * 2 }
// anonymous function, equivalent
val doubledA = ints.map(fun(x: Int): Int { return x * 2 })
println(doubledL) // [2, 4, 6]
println(doubledA) // [2, 4, 6]go deeper
Recognizes the fun(x: Int): Int { } form and can name the obvious syntax differences from a lambda.
Knows both expression and block body forms and that the return type can be inferred or declared.
Connects the syntactic choice to return semantics and explicit typing, choosing the form deliberately.
Frames anonymous functions vs lambdas as a readability/return-control tradeoff and guides team conventions.
## What is an anonymous function? Kotlin has two ways to write a **function literal** — a function value you can store in a variable or pass as an argument: 1. **Lambda expression** — `{ x: Int -> x * 2 }` 2. **Anonymous function** — `fun(x: Int): Int { return x * 2 }` An **anonymous function** looks like a normal `fun` declaration except it has **no name**. Everything else stays familiar: a parenthesized parameter list, an optional explicitly declared **return type**, and a body that may use the `return` keyword. ## Syntax comparison ```kotlin // Lambda: no fun keyword, params before -> inside braces, return type inferred val lambdaForm: (Int) -> Int = { x -> x * 2 } // Anonymous function, block body: fun keyword, explicit return type, return statement val anonBlock: (Int) -> Int = fun(x: Int): Int { return x * 2 } // Anonymous function, expression body: return type inferred from the expression val anonExpr: (Int) -> Int = fun(x: Int) = x * 2 ``` ## Key differences - **Keyword:** the anonymous function uses `fun`; the lambda uses none. - **Parameter types:** in an anonymous function you usually write parameter types explicitly. In a lambda they are often inferred from the expected type, and a single parameter can be referenced as the implicit `it`. - **Return type:** an anonymous function can declare it after the parameter list (`: Int`). A lambda always infers it. - **`return` semantics:** in an anonymous function, a bare `return` returns from the anonymous function itself (a *local* return). In a lambda, a bare `return` is a **non-local** return that returns from the enclosing function — this is the most important behavioral difference and the usual reason to choose an anonymous function. ## When you would reach for one Use an anonymous function when you want an explicit return type, or when you need a plain `return` that exits only the function literal (for example inside `forEach`) without resorting to a labeled `return@forEach`. Both forms compile to the same `Function` types (e.g. `(Int) -> Int`), so they are interchangeable at the call site.
- Can an anonymous function omit its return type?Yes — with an expression body (fun(x: Int) = x * 2) the return type is inferred. With a block body it defaults to Unit unless declared.
- Can you reference the parameter as 'it' in an anonymous function?No. 'it' is only available in single-parameter lambdas. Anonymous functions require named parameters.
A lambda is shorthand; an anonymous function is the same idea written out in full sentences.
saying these in an interview costs you the question
- Claiming anonymous functions and lambdas are identical in every way
- Saying you can use 'it' inside an anonymous function
- Thinking 'fun' before the parameter list is optional
- Not knowing the return keyword behaves differently between the two forms