Closures
Lesson 1 of 1
  1. 1
    Closures are self-contained chunks of code that are first-class citizens in Swift. This means they can be assigned to variables and passed as arguments to functions. Closures may be referred to as …
  2. 2
    Before we define a closure let’s recall the function definition from the Functions module. This will be helpful to reference while we step through the closure definition. func functionName(par…
  3. 3
    So far we have defined closures without arguments or return values. Just like with functions, it’s very useful for our closures to be able to accept inputs, perform tasks with the inputs, and retur…
  4. 4
    Closures are first-class citizens in Swift. This means they can be assigned to variables and passed as arguments to functions. Now that we’ve looked at assigning them to variables, let’s see how we…
  5. 5
    Up until this point we have been fairly explicit about defining the type of the closure, assigning it to a variable, and passing the variable to a function. With Swift being a strongly typed langua…
  6. 6
    Because closures are so common in Swift, there are quite a few niceties or “syntactic sugar” that are available to shorten your code. To explain these, let’s take a look at a function that takes a …
  7. 7
    A higher order function is a function that takes a closure as input or a functions that returns a closure. This is something we have defined ourselves already in the previous exercises. In this exe…
  8. 8
    Up until now we have passed parameters into our closures to perform operations on. With closures we can also capture variables and constants outside the scope of the closure. This is known as “clos…
  9. 9
    Great work! Closures are an important tool in Swift that allows you to write and use higher-ordered functions. In this lesson, you learned how to: - Define closures - Pass closures to functions …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo