TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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")
    first()
    print("About to start second work")
    second()
    print("About to start third work")
    third()
    print("Done!")
}

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?

Thanks!

2      

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 ...) { 
  boilWater() 
  firstStep()
  ...
}

when you call that function it will look like this:

makeHotBeverage {
    openBoxOfTeaLeaves()
}

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.
    openBoxOfCoffeeBeans()
    ....
}

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.

2      

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

2      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.