1. Introduction
When you write a local function for the first time, your brain is happy: “Nice! I can hide helper logic right next to where it’s used and not clutter the file.” And then you notice that the local function calmly reads variables from the outer function. And not only reads them—sometimes it changes them too.
That moment is called a closure: a local function “closes over” external variables that are available to it by scope.
A bit more formally (but without academic rigor), a closure is a situation where a function uses variables declared not inside it, but outside, in an outer block. Kotlin allows this: a closure includes variables from the outer scope, and such variables may be mutable.
Capturing val: “context” without surprises
The friendliest kind of closure is when a local function captures an external variable declared as val. Why friendly? Because you can’t reassign a val, so the context is stable: the function won’t end up in a situation like “today my prefix is one thing, tomorrow it’s another, because someone somewhere changed the value.”
Imagine we want to print messages with a single shared prefix. We could pass the prefix as a parameter every time, but that gets old fast. A local function solves the problem: the prefix is declared once, and log(...) uses it.
fun main() {
val prefix = "[INFO]"
fun log(message: String) {
println("$prefix $message")
}
log("Start") // [INFO] Start
log("Done") // [INFO] Done
}
Here, log is a local function, and prefix is an external variable available by scope. That’s a closure, but of the safe kind: we don’t mutate anything, we only read.
Capturing var: convenient, but hidden state appears
If a local function captures an external var, that variable becomes shared mutable state between the outer function and the inner one. This can be convenient (for example, to keep a counter), but the behavior starts depending on call order and on who changed the variable, and when.
Let’s start with the simplest example: a sum that we increase with a small local function.
fun main() {
var total = 0
fun add(x: Int) {
total += x
}
add(10)
add(5)
println(total) // 15
}
In this code, add mutates the external variable total. From the compiler’s point of view, this is fine: a closure can mutate captured variables.
But there’s a cost: if you see a call like add(10), the signature add(x: Int) doesn’t tell you that it changes an “external” sum. That means the state becomes hidden: it isn’t passed via parameters and it isn’t returned as a result; it just changes “somewhere in there.”
That’s not always bad. But next we’ll learn to feel the point where “convenient” starts turning into “why did it break, I didn’t touch anything.”
An important nuance: a closure sees the current value of var
A common beginner misconception sounds like this: “A local function kind of remembers the variable’s value at the moment it was declared.” In Kotlin, that’s not the case: the local function refers to the variable, and on every call it sees its current value.
Let’s check with a short example:
fun main() {
var mode = "A"
fun printMode() {
println("mode=$mode")
}
printMode() // mode=A
mode = "B"
printMode() // mode=B
}
This matters for understanding the risks: if mode changes in different places, then the behavior of printMode() can change too. Sometimes that’s exactly what you want (for example, a debug mode), but sometimes it turns into a mystery: “why is the log printing differently now?”
2. Practical example: a mini expense tracker
Now let’s build something closer to a “real app,” but still simple: a console mini expense tracker. For now, we won’t store a list of expenses (collections will come later); we’ll keep only aggregates: the sum and the number of entries. This is convenient for demonstrating closures: the session state lives in var variables, and local functions neatly use it.
We want this scenario: the user enters numbers (expenses), and we print the current total. To exit, they enter exit.
fun main() {
runExpenseSession()
}
private fun runExpenseSession() {
val currency = "USD"
var total = 0
var count = 0
fun printStatus() {
println("Entries=$count, total=$total $currency")
}
println("Enter expenses, or 'exit' to finish.")
printStatus()
}
Notice: printStatus() captures currency, total, count. currency is a val, and total/count are var. That’s already a closure. So far it’s used peacefully: it only reads.
Now let’s add input and parsing.
private fun runExpenseSession() {
val currency = "USD"
var total = 0
var count = 0
fun printStatus() {
println("Entries=$count, total=$total $currency")
}
while (true) {
val raw = readln().trim()
if (raw == "exit") break
val amount = raw.toIntOrNull()
if (amount == null) {
println("Not a number. Try again.")
continue
}
total += amount
count += 1
printStatus()
}
}
There’s an “implicit” point here: the state total and count changes in the loop body, but printStatus will always show the current values. This is convenient and predictable as long as you keep state mutations in one place and understand who is responsible for them.
Adding logging: a closure over val and over var
Let’s add a simple logger. We’ll start with the safe variant: the logger captures only the val prefix.
private fun runExpenseSession() {
val prefix = "[EXPENSE]"
fun log(text: String) {
println("$prefix $text")
}
log("Session started") // [EXPENSE] Session started
}
This reads as “the logger has a constant context.”
Now let’s add a counter for invalid inputs. This is capturing a var, and here it’s important not to lose control.
private fun runExpenseSession() {
var invalidInputs = 0
fun onInvalidInput(raw: String) {
invalidInputs += 1
println("Invalid: '$raw' (errors=$invalidInputs)")
}
val raw = readln().trim()
onInvalidInput(raw) // Invalid: '...' (errors=1)
}
In a tiny function, this is fine, but in larger code such a counter easily becomes a “ghost”: it grows somewhere inside, and it’s not always clear why.
3. Risks and readability: how not to turn code into a detective story
The word “testability” may sound like something from the world of big projects, but in practice it’s about a very simple thing: “can I understand and verify a function in isolation.” When a function depends only on parameters and its return value, it’s easy to mentally “run” it. When a function also mutates external vars, you have to keep extra conditions in your head: what the variable’s value was before the call, who could have changed it earlier, how many times you called this function.
Let’s compare two approaches with a toy example.
Option A: mutating external state (a closure over var):
private fun demoA() {
var sum = 0
fun add(x: Int) {
sum += x
}
add(2)
add(3)
println(sum) // 5
}
Option B: the function returns a new result (no hidden state):
private fun demoB() {
fun add(sum: Int, x: Int): Int {
return sum + x
}
var sum = 0
sum = add(sum, 2)
sum = add(sum, 3)
println(sum) // 5
}
Option B has more “routine” (passing sum back and forth), but the logic is transparent: you can see what changes and where.
A practical rule for beginners: closures with var are not “bad,” but they are a tool that’s easy to make invisible to the reader. And invisible things in code usually come back to bite you later.
How to use closures carefully
Closures become safer when you stick to one idea: dependencies should be visible to the reader. If a local function uses an external variable, that’s fine, but it’s better if it’s obvious at least from the name and the code’s layout.
Usually a few techniques help.
First, local functions that mutate external state are useful to name in a way that reads like an action on state: addToTotal, incrementInvalidCount, setModeDebug. Then the reader won’t expect a “pure function” and will immediately understand: something is about to change.
Second, if a local function can be written to take data as a parameter, that often makes the code clearer. Even if access to outer variables exists, a parameter turns the dependency into part of the contract: “I work with what I was given.”
Third, it helps to keep the entire “scenario” linear. A closure over var is especially dangerous when you mutate the same variable from different places: a little bit in one if, a little bit in another, and somewhere else in a local function. In a small example that’s tolerable, but in real code it quickly turns into a TV series with no ending.
4. Common mistakes when working with closures
Before we finish, it’s useful to call out the most common “rakes” that almost everyone steps on. Closures are tricky: in a small example they look cute and convenient, and then you add a couple more conditions and suddenly stop understanding why the program prints “the wrong thing.” These mistakes aren’t about syntax, but about style and clarity.
Mistake #1: a local function silently mutates an external var, and you can’t tell from the name.
When you have fun add(x: Int) and somewhere separately var total, the reader can easily interpret add as “just a computation” (especially if they’re used to mathematical functions). And then it turns out that add is an action that mutates state. If a function mutates an external variable, it’s better to name it so it reads like a side effect: addToTotal, increaseSum, registerExpense.
Mistake #2: the same var is mutated in multiple places, and order becomes critical.
The most unpleasant kind of hidden state is when the value depends on history: “how many times you called the function,” “which if branch ran first,” “whether there was an invalid input already.” This doesn’t mean you can’t do it, but then the code should remain linear and as simple as possible. Otherwise you’ll get a program where fixing one line breaks everything, like a house of cards.
Mistake #3: expecting a closure to remember an old variable value.
Sometimes a developer thinks: “I declared the function when mode was "A", so it will keep working like it’s in mode A.” But the local function sees the current value of the variable on each call. So after mode = "B" the behavior will change, and that’s normal. If you really need to “freeze” a value, then with our current tools the simplest approach is to store it in a separate val and use that inside.
Mistake #4: name shadowing that makes you read the wrong variable.
When outside you have var total, and inside you do fun add(total: Int), the brain gets confused even for experienced people. As a result, you may update the wrong thing, or simply misinterpret the code while reading it. Simple naming discipline (not repeating names in nested scopes) dramatically reduces the risk.
Mistake #5: a closure captures too many external variables and turns into a magic function.
If a local function uses five external variables, its behavior becomes hard to explain at the call site. It starts to look like “something that will figure it out.” Usually this is a sign that the local function should be simplified: either pass some dependencies as parameters, or split it into two small functions, each of which performs one step and uses minimal state.
GO TO FULL VERSION