Programming in Swift: Functions & Types

Jan 4 2022 · Swift 5.5, iOS 15, Xcode 13

Part 2: Closures

11. Challenge: Closures

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Closures Next episode: 12. Closure Syntax

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 11. Challenge: Closures

Update Notes: This course was originally recorded in 2019. It has been reviewed and all content and materials updated as of October 2021.

Transcript: 11. Challenge: Closures

I’ve got a quick challenge to help you practice writing closures. You’ll find it on page 3 of the Playground for this part of the course. Pause the video, give it a try, and then come back to go over some solutions. Have fun!

OK, first challenge.Create a closure version of this calculateFullName function. I’ll start with a new constant, and I’ll call it the same thing as the function name…

let fullName =

Then I need to set up a pair of curly braces to encapsulate this closure.

let fullName = {

}

Now, just like the function, this closure needs a parameter list and a return type. I can actually just copy it straight from the function.

let fullName = { 😺(firstName: String, lastName: String?) -> String🛑 
}

I just need to follow all of that up with the keyword in to mark the start of the closure’s body.

let fullName = { (firstName: String, lastName: String?) -> String 😺in 
}

Then, for the body, it should do exactly the same thing as the function, so, again, I can just copy and paste

  😺firstName + " " + (lastName ?? "")🛑

and that’s it! Now to try them both out. First the function, that will be the one with the argument labels. I’ll make Ozma’s full name.

let ozmaName = calculateFullName(firstName: "Ozma", lastName: "Catterwaul")

And then for the closure, I’ll make a full name for someone who doesn’t have a last name, liiiike Prince!

let princeName = fullName("Prince", nil)

Run the playground… and there’s both names over on the right. Nice! OK onto challenge number two.

Call printResult using an inline closure as an argument. Alright. I’ll start with printResult and let it auto-generate a closure for me.

printResult({ (<#Double#>, <#Double#>) -> Double in
  <#code#>
}, <#T##a: Double##Double#>, <#T##b: Double##Double#>)

I’ll name the parameters a and b…

printResult(
  { (😺a, b🛑) -> Double in

and in the body, you could have done whatever you wanted with the two numbers. But you’re bothering to write a closure, so it should be something you couldn’t do with an existing function.

I’ll use the pythagorean theorem to figure out what the longest side of a right triangle would be if the other two sides were passed into this function.

    ((a * a) + (b * b)).squareRoot()
  }

Because this is still just one statement, the result is returned for me. I don’t need to use the return keyword. And I can just pass in two numbers for the other arguments of printResult

printResult(
  { (a, b) -> Double in
    ((a * a) + (b * b)).squareRoot()
  }, 
😺5, 3🛑
)

Aaannnnddd run the playground! Done!