← Back to Kotlin Course | Chapter 4: Functions | Lesson 5 of 7

Local Functions

A local function is a helper recipe you define right inside another function, only for that function's own use.

Declaring a Local Function

A function defined inside another function's body is a local function, usable only within that enclosing function, which keeps small helpers scoped tightly to where they are needed.

Example: Declaring a Local Function

markup
fun printReport(total: Int) {
    fun formatCurrency(amount: Int): String = "$$amount.00"

    println("Total: ${formatCurrency(total)}")
}

fun main() {
    printReport(150)
}

Accessing Enclosing Variables

A local function can read variables from the function it is nested inside directly, without needing them passed in as parameters.

Example: Accessing Enclosing Variables

markup
fun calculateDiscountedTotal(prices: List<Int>, discountPercent: Int) {
    fun applyDiscount(price: Int): Int = price - (price * discountPercent / 100)

    val total = prices.sumBy { applyDiscount(it) }
    println("Discounted total: $total")
}

fun main() {
    calculateDiscountedTotal(listOf(100, 200, 300), 10)
}

Breaking Up Long Functions

Extracting repeated or logically distinct steps into local functions keeps the outer function's body focused on the overall flow, while the details live in named, testable-in-isolation pieces.

Example: Breaking Up Long Functions

markup
fun processOrder(itemCount: Int, pricePerItem: Int) {
    fun subtotal() = itemCount * pricePerItem
    fun tax(amount: Int) = amount / 10

    val sub = subtotal()
    println("Subtotal: $sub, Tax: ${tax(sub)}")
}

fun main() {
    processOrder(3, 50)
}

When to Avoid Local Functions

If a helper needs to be reused across several unrelated functions, it should be a regular top-level (or member) function instead of a local one, since local functions cannot be called from outside their enclosing function.

Example: When to Avoid Local Functions

markup
fun square(x: Int) = x * x

fun sumOfSquares(a: Int, b: Int): Int {
    return square(a) + square(b)
}

fun main() {
    println("Sum of squares: ${sumOfSquares(3, 4)}")
}
Common Mistakes
  1. Making a helper a top-level function when it is only ever needed inside one other function, cluttering the file's namespace.
  2. Forgetting that a local function can directly access the variables of its enclosing function, leading to redundant parameters being passed in.
  3. Nesting local functions too deeply, hurting readability instead of helping it.
Chapter Summary
  • A local function is declared inside another function's body and is only visible there.
  • Local functions can directly read (and, if var, modify) variables from their enclosing function's scope.
  • They help break a long function into smaller named pieces without polluting the outer file scope.
  • Overusing deeply nested local functions can hurt readability just as much as one giant function.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.