0Pricing
Swift Academy · Lesson

Functions as First-Class Values

Storing functions in variables, passing as arguments and returning from functions.

Functions as First-Class Values is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

In Swift, functions are first-class citizens — you can store them in variables, pass them as arguments, return them from other functions, and nest them inside other functions.

Function Types

Every function has a type defined by its parameter and return types: ```swift func add(_ a: Int, _ b: Int) -> Int { a + b } let operation: (Int, Int) -> Int = add print(operation(3, 4)) // 7 ``` The type of `add` is `(Int, Int) -> Int`.

Passing Functions as Arguments

```swift func apply(_ f: (Int) -> Int, to value: Int) -> Int { f(value) } func double(_ n: Int) -> Int { n * 2 } print(apply(double, to: 5)) // 10 ``` This is the foundation of higher-order functions like `map` and `filter`.

Returning Functions

```swift func multiplier(by factor: Int) -> (Int) -> Int { return { value in value * factor } } let triple = multiplier(by: 3) print(triple(7)) // 21 ``` Returning a function creates a *closure* that captures `factor`.

Nested Functions

Functions can be defined inside other functions: ```swift func chooseStep(goForward: Bool) -> (Int) -> Int { func forward(_ n: Int) -> Int { n + 1 } func backward(_ n: Int) -> Int { n - 1 } return goForward ? forward : backward } let step = chooseStep(goForward: true) print(step(3)) // 4 ```

Closures as Shorthand

Anonymous closure literals can stand in for any function type: ```swift let square: (Int) -> Int = { $0 * $0 } print(square(6)) // 36 let doubled = [1, 2, 3].map { $0 * 2 } // [2, 4, 6] ``` `$0` refers to the first argument in the shorthand closure form.

Storing Functions in Collections

```swift let operations: [(Int, Int) -> Int] = [ { $0 + $1 }, { $0 - $1 }, { $0 * $1 } ] for op in operations { print(op(10, 3)) } // 13, 7, 30 ```

Trailing Closure Syntax

When the last argument is a function, you can write it outside the parentheses: ```swift func withLogging(_ label: String, action: () -> Void) { print("Start: \(label)") action() print("End: \(label)") } withLogging("fetch") { fetchData() } ```

Function Identity and Equality

You cannot compare two function values with `==` in Swift — functions are not Equatable: ```swift // let same = add == add ❌ ``` If you need to compare, wrap the function in a struct or class that conforms to Equatable.

Currying Pattern

Simulate currying by returning partially-applied functions: ```swift func add(_ a: Int) -> (Int) -> Int { { b in a + b } } let add5 = add(5) print(add5(3)) // 8 print(add5(10)) // 15 ``` Useful for creating specialised functions from generic ones.

Quick Check

What is the function type for a function that takes two `Int`s and returns an `Int`?

Recap

Key takeaways: • Functions have types like `(ParamTypes) -> ReturnType` • Store in variables, pass as arguments, return from functions • Closures (`{ }`) are anonymous function values • Trailing closure syntax cleans up call sites • Returning functions from functions creates closures that capture variables Next: the optional chaining and unwrapping lesson.

Frequently asked questions

Is the “Functions as First-Class Values” lesson free?

Yes — the full text of “Functions as First-Class Values” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Functions as First-Class Values”?

Storing functions in variables, passing as arguments and returning from functions. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Functions as First-Class Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Argument Labels and Parameter Names
  2. Default and Variadic Parameters
  3. In-Out Parameters and Multiple Returns
  4. Functions as First-Class Values
← Back to Swift Academy