Rudiger Rudiger - 2 years ago 104
Swift Question

Swift: definition and syntax of functions vs. closures

Is this a function or a closure?

let triple: Int -> Int = {
(number: Int) in // What is this?
let result = 3 * number
return result


Answer Source

1: It's a closure.


func aFunction() -> () {
    // do something

let aClosure:() -> () = {
    // do something

Functions are actually a special case of closures. You can write a closure without a name by surrounding code with braces ({}). Use in to separate the arguments and return type from the body.

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.

2: "in" is just the notation chosen to represent where the arguments/return type end, and the body of the closure begins. It has nothing to do with inout.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download