# Closures

## Overview

* Closures are the single toughest thing to learn in Swift ([HWS](https://www.hackingwithswift.com/quick-start/beginners/summary-closures)).
* Closure is lean version of Function
* Closure can be used in Function
* Services like Siri and Network Connections use Closures in an app to make queries faster. ([HWS](https://www.hackingwithswift.com/quick-start/understanding-swift/why-would-you-want-to-use-closures-as-parameters))

### How closures return a value

Here Closure returns nothing

```swift
let payment = { (user: String) in
    print("Paying \(user)…")
}
```

Here Closure returns value

```swift
let payment = { (user: String) -> Bool in
    print("Paying \(user)…")
    return true
}
```

### Functions vs. Closures

```swift
var invoice_amount = 20.0

// ----------------------------

func VAT_Incl_20_a (sum_wo_vat: Double) -> Double {
    sum_wo_vat * 1.2
}

VAT_Incl_20_a(sum_wo_vat:invoice_amount)
// output: 24
print("VAT Incl \(VAT_Incl_20_a(sum_wo_vat: 20))")
// print: VAT Incl 24.0

print("---")


let VAT_Incl_20_b = {(sum_wo_vat: Double) in
    print("VAT Incl \((sum_wo_vat)*1.2)")
}

VAT_Incl_20_b(invoice_amount)
// print: VAT Incl 24.0
```

### Trailing Closure

If a function's final parameters are functions, use trailing closure syntax ([HWS](https://www.hackingwithswift.com/quick-start/beginners/summary-closures)).

### Shorthand Parameters

Use it in very specific conditions.

## Sample Codes

{% code title="With String" %}

```swift
let fixCar = { (problem: String) in
    print("I fixed the \(problem).")
}
fixCar("ignition")

// print: I fixed the ignition.
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

{% code title="With Integer" %}

```swift
let makeReservation = { (people: Int) in
    print("I'd like a table for \(people), please.")
}
makeReservation(2)

// print: I'd like a table for 2, please.
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

{% code title="With Case Conditional" %}

```swift
var cutGrass = { (currentLength: Int) in
    switch currentLength {
    case 0...1:
        print("That's too short")
    case 2...3:
        print("It's already the right length")
    default:
        print("That's perfect.")
    }
}
cutGrass(2)

// print: It's already the right length
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

{% code title="With for" %}

```swift
let rowBoat = { (distance: Int) in
    for _ in 1...distance {
        print("I'm rowing 1km.")
    }
}
rowBoat(3)

/* 
print: 

I'm rowing 1km.
I'm rowing 1km.
I'm rowing 1km.

*/
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

{% code title="Returning with if-else conditional" %}

```swift
var flyDrone = { (hasPermit: Bool) -> Bool in
    if hasPermit {
        print("Let's find somewhere safe!")
        return true
    }
    print("That's against the law.")
    return false
}

flyDrone(true)
flyDrone(false)

/* 
print: 

Let's find somewhere safe!
That's against the law.

*/
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

{% code title="Returning with switch-case conditional" %}

```swift
let measureSize = { (inches: Int) -> String in
    switch inches {
    case 0...26:
        return "XS"
    case 27...30:
        return "S"
    case 31...34:
        return "M"
    case 35...38:
        return "L"
    default:
        return "XL"
    }
}
measureSize(36) // return: L
print(measureSize(36)) // print: L
```

{% endcode %}

**Source:** <https://www.hackingwithswift.com/review/sixty/accepting-parameters-in-a-closure>

## Sources

### Videos

{% embed url="<https://youtu.be/F68lyXkkfCY>" %}

{% embed url="<https://www.youtube.com/watch?v=ND44vQ5iJyc>" %}

### Articles / Documents

* [Hacking With Swift / 100 days / day-9](https://www.hackingwithswift.com/100/swiftui/9)
