Rudiger
Rudiger

Reputation: 6864

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
    number
    return result
}

triple(1)

Upvotes: 9

Views: 4637

Answers (5)

ScottyBlades
ScottyBlades

Reputation: 14083

Both. A function is a closure that has a name. The closure you have provided has a name and is therefore both a function and a closure.

From Apple's developer website:

Global and nested functions, as introduced in Functions, are actually special cases of closures. Closures take one of three forms:

Global functions are closures that have a name and do not capture any values. Nested functions are closures that have a name and can capture values from their enclosing function. Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.

Accordingly, and technically all functions are either global or nested, and all functions either take values or don't, therefore all functions are "special cases" of closures, therefore all functions are closures. However, not all closures are functions. There are cases where closures are non-named, especially when passing closures as arguments such as for the animate(withDuration: animations:) method.

UIView.animate(withDuration: 10) {
  // Here is an un-named closure you are passing 
  // in as an argument to the animate(withDuration: animations:) method
}

Upvotes: 4

Poles
Poles

Reputation: 3682

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.

Upvotes: 4

hallski
hallski

Reputation: 128109

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

Upvotes: 6

Conrad Taylor
Conrad Taylor

Reputation: 121

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

Upvotes: 5

Mick MacCallum
Mick MacCallum

Reputation: 130222

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.

Upvotes: 10

Related Questions