Swift: definition and syntax of functions vs. closures

1: It's a closure.

Examples:

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. https://itun.es/us/jEUH0.l

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.


Swift Closures are defined as follows:

{ (parameters) -> return type in
    statements
}

Thus, your code sample is considered a closure by definition. However, I would rewrite it as follows by moving the return type within the curly braces:

let triple = {
  (number: Int) -> Int in
  let result = 3 * number  
  number
  return result
}

triple(1)

The parameters and return type begin after the opening curly brace and end before the in keyword and the body of the closure begins after the in keyword and ends at the closing curly brace. Next, I would recommend downloading and reading iBook from iTunes which can be found at the following location:

https://itunes.apple.com/us/book/swift-programming-language/id881256329?mt=11


That's binding the closure to the constant triple which if done in the global scope is similar to declaring it as a function (see comment by Eric below for differences). The (number: Int) in line declares the input parameters to the closure.

In this case the type can be left out though since it can be inferred from the closure type, and since it only have one statement it can be written as a closure expression with implicit return as:

let triple: Int -> Int = { number in
    3 * number
}

// or as a function
func triple(number: Int) -> Int {
    return 3 * number
}

// Or as the very short
let triple: Int -> Int = { 3 * $0 }

You can read more about it in the official documentations chapter about closures.

https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html


A easy way to distinguish between functions and closures is,

Global Function: Have a name. Do not capture any values.

Nested Function: Have a name. Capture values from enclosing function.

Closure: Doesn't have name like function. Capture values from the adjacent blocks.