Making our own function

Earlier in the chapter, we looked at how one could write code that simulates the making of a pizza. The code looked like this:

print("Flattening the dough to form a round pizza Making our own function")
print("Adding some tomato sauce Making our own function")
print("Adding some mozzarella cheese Making our own function")
print("Adding some spicy pepperoni Making our own function")
print("Preparing the pizza in the oven Making our own function")
print("Done! Delicious pizza ready to be eaten Making our own function")

We discussed how this will scale if one would like to make many pizzas and the issues associated with that. Let's now look at how we can solve this by abstracting the preceding code using a function.

Go ahead and create a new Xcode playground and type in the preceding code:

Making our own function

The first thing we can do is to move our lines of code into a function body. Let's name our function makePizza for now. The function shouldn't take in any parameters and it will not return anything:

func makePizza() {
    print("Flattening the dough to form a round pizza Making our own function")
    print("Adding some tomato sauce Making our own function")
    print("Adding some mozzarella cheese Making our own function")
    print("Adding some spicy pepperoni Making our own function")
    print("Preparing the pizza in the oven Making our own function")
    print("Done! Delicious pizza ready to be eaten Making our own function")
}

To call this function, all we need to do is reference the function by its name, makePizza(), outside the function body. You should have the following code in your playground at this point:

Making our own function

Recall how Xcode runs our program, line by line from the top to the bottom of our playground; using functions breaks this pattern. In fact, our code for creating our pizza is not run before our function is called. This can quickly be verified by removing the call to our function (makePizza()) and see that nothing is printed to the console. Basically, the code inside a function body will not be run by Xcode before the function is called.

Let's iterate on our function by adding some parameters to specify the type of ingredients for our pizza and also, let's make it return the pizza in the form of a string describing our pizza. Most pizzas have tomato sauce and mozzarella cheese on them, but they can differ a lot in the rest of the ingredients; let's accommodate this by specifying all the ingredients besides tomato sauce and mozzarella cheese in a parameter to our function:

func makePizza(ingredients: String) -> String {
    print("Flattening the dough to form a round pizza Making our own function")
    print("Adding some tomato sauce Making our own function")
    print("Adding some mozzarella cheese Making our own function")
    print("Adding some \(ingredients)")
    print("Preparing the pizza in the oven Making our own function")
    print("Done! Delicious pizza ready to be eaten Making our own function")
    return "Pizza with \(ingredients)" 
}

Now that our function takes in a parameter and returns a value of the String type, we need to change the way we call our function. At the same time, because our function now returns a value, we can assign that value to a constant:

let myPizza = makePizza(ingredients: "meatballs Making our own function")

If we take a look at the console, we can see that the pizza being made is using the ingredients we specified in the function call:

Making our own function

With our pizza-making function, it is now very easy to make multiple pizzas:

let pizzaWithMeatballs = makePizza(ingredients: "meatballs Making our own function")
let pizzaWithSlicedHam = makePizza(ingredients: "sliced ham Making our own function")
let pizzaWithShrimps = makePizza(ingredients: "shrimps Making our own function")

Note, how we managed to remove a lot of duplicate code compared to what we did earlier in this chapter. We now have a centralized place for making a pizza, and any changes to that only need to be done once. Further, we have a way of making pizzas with different ingredients without having to change the fundamentals of a pizza being made.

The return value of our function makes it possible for us to store the different pizzas for later references in our code. In fact, suppose we print our different pizzas to the console, as shown:

print(pizzaWithMeatballs)
print(pizzaWithSlicedHam)
print(pizzaWithShrimps)

We should see something like this at the bottom of our console as a result:

Making our own function