LAST CHANCE: Save 50% on all my Swift books and bundles! >>

Day 9 - accept functions as parameters course question

Forums > 100 Days of SwiftUI

Hey! It's the wonderpig again 😅

I'm following day 9 on the accepting functions as parameters but I dont understand the last example with the "doImportantWork" function.

I followed quite nicely up until that point but I don't really understand what the code he makes us write does or even why I should care about this.

If I understand the bellow code is creating a function that contain functions returning nothing as parameters.

func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) {
    print("About to start first work")
    print("About to start second work")
    print("About to start third work")

But then the second part of the code is doing what? Defining what the enclosed functions (first, second and third) do? I'm kinda lost here 🤣

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

Has any of you understood this part of the lesson?



Think of it as a list of tasks you have to do to process something. That function accepts three another functions and does something with them. But before each of the passed functions, let's say you need to do some extra work. So before you handle first function passed, you need to do something, prepare data, make calculations... etc. Using an analogy you can imagine function prepare hot beverage. That function accepts other function: open a box of tea leaves, but before you open that box and do something with it you need to boil water... etc... so it will look something like this

func makeHotBeverage(firstStep: () -> Void ...) { 

when you call that function it will look like this:

makeHotBeverage {

you use openBoxOfTeaLeaves() here but this function signature i.e. its type is () -> Void. If next time you want to make coffe you can use it. So it becomes reusable. Boiled water is necessary for coffee too...

makeHotBeverage {
    // Water is boiled prior next step is executed as you put that in function declaration.

Paul is giving simple print statements in order to avoid writing such long code with different functions. You need to undestandand that you can pass functions in to other functions and order them in certain way as well you can use them as closures.


Ok I think I get it a bit better now. Thanks a lot friend 😊


Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.