revidwerd

revidwerd

Day 5 of #100DaysOfSwiftUI was all about functions and their various incantations. This included returning values (tuples are great for returning more than 1 valule!), accepting parameters, labeling parameters for better legibility (omitting labels in some cases), setting default parameters, “inout” parameters, “throwing functions,” and variadic functions (passing in 0 or more values).

Ternary operators shine in SwiftUI as it uses single expression functions quite a bit:

func greet(name: String) -> String {
        name == "Drew Diver" ? "My liege..." : "Hello, \(name)! Back of the line!"
}

Parameter labels make function calls read naturally:

func sayHello(to name: String) {
        print("Hello, \(name)!")
}

sayHello(to: "Drew Diver”)

Omitting parameter labels makes sense in cases like the following:

func read(_ book: String) {
        print("Now reading \(book)...")
}

read("Walden")

Also In Objective-C:

…the first parameter to a function was always left unnamed, and so when you use those frameworks in Swift you’ll see lots of functions that have underscores for their first parameter label to preserve interoperability with Objective-C.