DEV Community

Lou Franco
Lou Franco

Posted on • Edited on • Originally published at app-o-mat.com

The Swift Programming Language Companion: Functions (Part 2)

In the last article, I gave some exercises for functions. Here are some more to cover the more advanced features.

For these exercises, we're going to use exercises imagined around a diet tracker. In your playground copy this code

let foodCalories = [
    "Apple": 72,
    "Bagel": 289,
    "Banana": 105,
    "Carrots": 52,
    "Egg": 102,
    "Butter": 102,
    "Cookie": 150,
    "Cola": 136,
    "Water": 0,
]
Enter fullscreen mode Exit fullscreen mode

This code:

let food = foodCalories.randomElement()
food?.key
Enter fullscreen mode Exit fullscreen mode

Will tell you a random food from the list. food?.key is of type String? because if food is an empty dictionary, we won't be able to find a randomElement.

This code:

let firstFood = foodCalories.first
firstFood?.key

Enter fullscreen mode Exit fullscreen mode

Will tell you the "first" food from the list, which is also kind of random (but should be the same each call), because dictionaries don't have an order.

Now:

  1. Make a function that takes the foodCalories dictionary as a parameter and return a random food.

    func randomFood(foodCalories: [String: Int]) -> String?

  2. Take the first function, copy it, and change it so that you don't have to use a label for the first argument. Change it so that we can call it like randomFood(foodCalories) instead of randomFood(foodCalories: foodCalories)

  3. Take the first function, copy it and change it so that you use from as a label for the first argument. Change it so that we can call it like randomFood(from: foodCalories).

  4. Make a function that takes the foodCalories dictionary as a parameter and returns the "first" food.

    func firstFood(foodCalories: [String: Int]) -> String?

  5. This one is hard ... Make a function that takes a function of the type that randomFood and firstFood are, and calls it three times and returns a String array with the results of the three calls.

Try the last one. If you need help, read on ...

The type is ([String: Int]) -> String?. This means, "a function that takes a dictionary and returns an optional Stringโ€. Both randomFood and firstFood match this type. You can make a function that takes this type as a parameter.

Try it ...

Ok, here's the function declaration

func threeFoods(foodCalories: [String: Int], foodPicker: ([String: Int]) -> String?) -> [String] {
   return []
}
Enter fullscreen mode Exit fullscreen mode

You can call this function like this:

threeFoods(foodCalories: foodCalories, foodPicker: randomFood(foodCalories:))

This says -- give me three foods from the foodCalories dictionary using the randomFood function to pick them.

Fill in the threeFoods function so it does this. Inside that function, foodPicker is a function you can call. Functions can be passed to functions, just like Strings, Ints, etc.

There are tricky things here regarding optionals and building the array. DM me or comment below if you need help.

Next:

Now that we know the basic types, collection types, control flow, and functions. For the next few chapters I will give exercises that review all of those topics before moving on.

Top comments (0)