Skip to content
Justice Tech School

Nested Functions and Scope

Functions can live inside functions and capture local variables.

Example
func counter() -> () -> Int {
    var n = 0
    func next() -> Int { n += 1; return n }
    return next
}
let c = counter()
print(c(), c(), c())

Exercise

Inner function.

func outer() {  inner() {} }

Quiz Test

What does inner keep?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.