# Functions

## Overview

### Benefits of using Functions

* **Clearer code**
  * Breaking big chunks of code into small pieces, so that you read and follow the code easily
  * Reusability in other functions, therefore you can write clearer code
* **Ease of change**
  * When used in multiple places and other big functions, you change the behavior of the function from a place with ease

### Things to Consider

* Not to add more than 6 parameters
* Splitting up into smaller functions for sake of readability
* Whether those parameters are related to each other

### Throwing Data back

If you want to return your *own* value from a function, you need to do two things:&#x20;

1. Write an arrow then a data type before your function’s opening brace, which tells Swift what kind of data will get sent back.
2. Use the `return` keyword to send back your data.

```swift
// Standart if-else conditional
func greet(name: String) -> String {
    if name == "Taylor Swift" {
        return "Oh wow!"
    } else {
        return "Hello, \(name)"
    }
}

print(greet(name:"Sedat"))

print("-------------------------------------------------------------------")

// Ternary Conditional
func greet2(name: String) -> String {
    name == "Taylor Swift" ? "Oh wow!" : "Hello, \(name)"
}

print(greet2(name:"Sedat"))

print("-------------------------------------------------------------------")

// without return, it does not output the result outside of the function
func greet3(name: String) {
    if name == "Taylor Swift" {
        "Oh wow!"
    } else {
        "Hello, \(name)"
    }
}

print(greet3(name:"Sedat"))
```

### [Tuples](https://swift.sedatonat.dev/codes/data-collectors/tuples)

### Copying Functions

When you copy a function you do not use parenthesis ()

```swift
greetUser()
var greetCopy = greetUser
greetCopy()
```

**Source:** [HWS](https://www.hackingwithswift.com/quick-start/beginners/how-to-create-and-use-closures),&#x20;

When copying functions parameters will not come externally??? #learn

### Removing Parameter Name

```swift
// With parameter name
func climbMountain0(name: String) {
    print("I'm going to climb \(name).")
}
climbMountain0(name:"Everest")

/*
print:

I'm going to climb Everest.

*/


print("-------------------------------------------------------------------")

// Without parameter name
func climbMountain1(_ name: String) {
    print("I'm going to climb \(name).")
}
climbMountain1("Everest")

/*
print:

I'm going to climb Everest.

*/
```

No difference in result at all.

### Defining a Default Value for a Parameter

```swift
/* 
In "end: Int = 3" 3 is the default value. 
If nothing is defined for the "end" value so 3 is the value
*/

func printTimesTables(for number: Int, end: Int = 3) {
    for i in 1...end {
        print("\(i) x \(number) is \(i * number)")
    }
}

//printTimesTables(for: 5, end: 20)
printTimesTables(for: 8)

/*
print:

1 x 8 is 8
2 x 8 is 16
3 x 8 is 24

*/
```

**Source:** [HWS](https://www.hackingwithswift.com/quick-start/beginners/how-to-provide-default-values-for-parameters),&#x20;

### Checking Errors

If a Function may throw an error, you must add error handling both to prevent crashing the app and to clarify the cause.

But keep in mind that adding a throw to function may cause many other problems. So it is better not to add them at the beginning. By the time you become familiar with the app itself and the throw mechanism, you will ad them carefully.

```swift
enum PasswordError: Error {
    case short, obvious
}

func checkPassword(_ password: String) throws -> String {
    if password.count < 5 {
        throw PasswordError.short
    }

    if password == "12345" {
        throw PasswordError.obvious
    }

    if password.count < 8 {
        return "OK"
    } else if password.count < 10 {
        return "Good"
    } else {
        return "Excellent"
    }
}


let string = "12345"

do {
    let result = try checkPassword(string)
    print("Password rating: \(result)")
} catch PasswordError.short {
    print("Please use a longer password.")
} catch PasswordError.obvious {
    print("I have the same combination on my luggage!")
} catch {
    print("There was an error: \(error.localizedDescription)")
}

/*
print:

I have the same combination on my luggage!

*/
```

**Source:** [HWS](https://www.hackingwithswift.com/quick-start/beginners/how-to-handle-errors-in-functions),&#x20;

### Handling Function Failure with Optionals

If you only care about whether the function works or not, try may help. Otherwise do not use it.

If you want to know what those error are, and handle them uniquely, do not use "try?", but use do-catch-try block instead.

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

#### **Source:** [HWS](https://www.hackingwithswift.com/quick-start/beginners/how-to-handle-function-failure-with-optionals),&#x20;

### Methods

When Functions belong to Structs, they become Methods.

#### Sources

* [Hacking With Swift / What’s the difference between a function and a method?](https://www.hackingwithswift.com/quick-start/understanding-swift/whats-the-difference-between-a-function-and-a-method)
* [Swift.org / Methods](https://docs.swift.org/swift-book/LanguageGuide/Methods.html)

### Mutating Function

Gives you ability to change a constant in a struct ([HWS / 100days / 10](https://www.hackingwithswift.com/quick-start/beginners/how-to-create-your-own-structs)).

* <mark style="color:red;">// But why to change it? If we need to change it afterwards why we not define it as a var? #learn</mark>

#### Example 1: Resetting the the value set to its defaults

```swift
struct Canvas {
    var backgroundColor: Color?
    var foregroundColor: Color?
    var shapes = [Shape]()
    var images = [Image]()

    mutating func reset() {
        self = Canvas()
    }
}
```

####

#### Sources

**Videos**

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

**Articles / Documents**

* [Swift by Sundel / Mutating and non-mutating Swift contexts](https://www.swiftbysundell.com/articles/mutating-and-nonmutating-swift-contexts/)
* [Hacking With Swift / Why do we need to mark some methods as mutating?](https://www.hackingwithswift.com/quick-start/understanding-swift/why-do-we-need-to-mark-some-methods-as-mutating)
* [Hacking With Swift / 100 days / 10 - How to create your own structs](https://www.hackingwithswift.com/quick-start/beginners/how-to-create-your-own-structs)
* [Page: Mutating Func](#mutating-function)

### Functions as Parameters

```swift
func makeArray(size: Int, using generator: () -> Int) -> [Int] {
    var numbers = [Int]()

    for _ in 0..<size {
        let newNumber = generator()
        numbers.append(newNumber)
    }

    return numbers  // output: [14, 17, 10, 11, 14]
}

let rolls = makeArray(size: 5) {  // output: [14, 17, 10, 11, 14]
    Int.random(in: 1...20)
}

print(rolls)
// print: [14, 17, 10, 11, 14]
```

```swift
func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) {
    print("About to start first work")  // output: About to start first work
    first()
    print("About to start second work")  // output: About to start second work
    second()
    print("About to start third work")  // output: About to start third work
    third()
    print("Done!")  // output: Done!
}

doImportantWork {
    print("This is the first work")
} second: {
    print("This is the second work")
} third: {
    print("This is the third work")
}

/*
print:

About to start first work
This is the first work
About to start second work
This is the second work
About to start third work
This is the third work
Done!

*/
```

```swift
var payCash = {
    print("Here's the money.")
}
func buyClothes(item: String, using payment: () -> Void) {
    print("I'll take this \(item).")
    payment()
}
buyClothes(item: "jacket", using: payCash)

/*
print:

I'll take this jacket.
Here's the money.

*/
```

**Source:** <https://www.hackingwithswift.com/quick-start/beginners/how-to-accept-functions-as-parameters>

## Sample Codes

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FwsupHWzuvsGCZwJeOZT2%2Fimage.png?alt=media\&token=f8a74493-340d-4684-81e2-8f25bbbd97d4)

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FmfQ45i6v26MWBJgQO1Sy%2Fimage.png?alt=media\&token=bd02655f-6a4a-4ece-a3bc-e38081fb83cb)

```swift
func square(numbers: [Int]) {
    for number in numbers {
        let squared = number * number
        print("\(number) squared is \(squared).")
    }
}
square(numbers: [2, 3, 4])

/*
print:

2 squared is 4.
3 squared is 9.
4 squared is 16.

*/
```

## Sources

### Videos

### Articles / Documents

* <https://www.hackingwithswift.com/100/swiftui/7>
* <https://docs.swift.org/swift-book/LanguageGuide/Functions.html>
* <https://www.kodeco.com/28797163-your-first-ios-swiftui-app-an-app-from-scratch/lessons/18>
* [Swift.org / Methods](https://docs.swift.org/swift-book/LanguageGuide/Methods.html)
