It’s a new day, which means another day of 100 Days of Swift. Today was all about functions. Like previous days, I knew the most obvious bits pretty well, but I still learned some useful tidbits.

First of all, while I was aware of the _ syntax for omitting parameter names, I wasn’t explicitly aware that it was part of a larger system of parameter labels. This is probably easiest to understand in the context of prepositions:

func give(_ gift: String, to recipient: String) {
    print("\(recipient) thanks you for the \(gift).")
give("Baseball Tickets", to: "Erika")

Another piece of syntax that was new to me was default parameters:

enum PlayStyle {
    case AD
    case AP

func playKennen(_ style: PlayStyle = .AP) {
    if style == .AP {
        print("Win hard or lose hard")
    } else {
        print("Tank killer")


Something else that I knew was possible, but didn’t know the syntax for was variadic functions. It feels a little odd to me that the parameter type magically changes to a list, but I guess it makes sense:

func gratefulFor(_ gratitude: String...) {
    for gratitudeItem in gratitude {
        print("Today I'm grateful for \(gratitudeItem).")
gratefulFor("coffee", "swift", "wwdc videos")

Finally, while I’d seen inout function parameters once or twice, and I knew of call by reference for errors and such in Objective-C, I hadn’t seen the syntax for declaring a function with an inout parameter in Swift yet. It’s not something that I think I’ll need very often, but here’s an example (adapted from Stack Overflow):

struct Vector3 {
    let x: Float;
    let y: Float;
    let z: Float;

func + (left: Vector3, right: Vector3) -> Vector3 {
    return Vector3(x: left.x + right.x, y: left.y + right.y, z: left.z + right.z)
func += (left: inout Vector3, right: Vector3) {
    left = left + right

var first = Vector3(x: 1, y: 3, z: 2)
let second = Vector3(x: 3, y: 2, z: 1)
first += second

With that, I’m off to spend the next 30 minutes of my day with Swift working on MFArtist.