KnowledgeCity

Swift: Functions and Closures

The topics discussed in this course will build on your understanding of the Swift programming language by discussing and explaining functions and…

The topics discussed in this course will build on your understanding of the Swift programming language by discussing and explaining functions and closures. To create usable apps, it is essential that you have a firm understanding of functions and closures, as well as how to implement them. One of the more difficult concepts of Swift to understand is closure, which is why this course will go through basic closures, spend time on how to accept a parameter in a closure, and then see how to return a value from a closure. 

In Swift: Functions and Closures, you will learn about functions in Swift, including closures and how to utilize them in your code. We’ll look at overloading functions, which are functions that have the same name as another function, but with a different set of parameters. We’ll also work on variadic functions and look at how to use a function as a parameter in another function. 

Learning Objectives:

  • Understand overloading functions
  • Recognize variadic functions
  • Understand closures and how to accept parameters in a closure

Author: Mark Nair

Duration: 34m · 7 lessons
Level: Advanced
Language: English

Skills you’ll gain

Function OverloadingFunctional ProgrammingGeneric FunctionStored FunctionsSwift (Programming Language)User Defined Functions

What You'll Learn

  • Understand overloading functions — functions sharing a name but with a different set of parameters
  • Recognize and work with variadic functions in Swift
  • Use a function as a parameter inside another function
  • Write basic closures and accept parameters within a closure
  • Return values from a closure
  • Use closures as function parameters

Key Takeaways

  • This course builds on your understanding of the Swift programming language by explaining functions and closures.
  • A firm understanding of functions and closures, and how to implement them, is essential to creating usable apps.
  • Closures are one of the more difficult Swift concepts, so the course covers basic closures, accepting a parameter in a closure, and returning a value from a closure.
  • Overloading functions are functions that have the same name as another function but with a different set of parameters.
  • The course also covers variadic functions and using a function as a parameter in another function.

Frequently Asked Questions

What does this course cover?

It covers functions and closures in Swift, including overloading functions, variadic functions, using functions as parameters, basic closures, accepting parameters in a closure, and returning values from a closure.

Who is this course for?

It is for learners building on their understanding of the Swift programming language who want a firm grasp of functions and closures and how to implement them to create usable apps.

What are overloading functions?

Overloading functions are functions that have the same name as another function but with a different set of parameters.

Why does the course spend time on closures?

Closures are described as one of the more difficult concepts of Swift to understand, so the course works through basic closures, how to accept a parameter in a closure, and how to return a value from a closure.

What skills will I gain from this course?

Skills include function overloading, functional programming, generic functions, stored functions, Swift programming, and user-defined functions.

Transcript

Show transcript (free preview lesson)

Transcript of the free preview lesson. Remaining lessons unlock with the full course.

Hello, my name is Mark Nair, and in these lessons you'll learn about functions and closures in Swift. On first looking at how we overload a function, so overloading a function means you make different functions of the same name, you just implement them in a different way. So let's look at a few examples. I'll make a function called birthdaygreens. So very simple function, let's call that, and when I call it you see in the popup, I have this one option, which is birthday greetings two, birthday greetings two, Mark, and just to quickly go over it, keyword, the name of the function, the label, the name of my argument, the type, let's overload this function. I'll make a new function, and I will call it birthday greetings. Now this is identical to what we had before, but this time I'm going to add a new parameter from friend. So these two functions, same name, right here. I'm overloading it, but in this case I have a different parameter, a new argument in here. Now when I call the function, in my popup, I have birthday greetings two, birthday greetings two, and from, Charlie wishes you a happy birthday Sally. Let's overload this function yet again, but this time with a different type as a parameter. Now in this case, I'm just going to copy this. Now two and from, instead of from, I'll just use age, and now I have a different type inside of my parameter list, and there we go. Now when I call the function, birthday greetings two, birthday greetings two, from, birthday greetings two, from two and age, and you'll see in the description of the very bottom of this popup window, two is string, two and from, type string, two, type string, ages of type int, exactly what we said I'm on 11. Let's overload this function yet again, this time I'm going to copy this whole thing here. I'm going to overload it with the change of the label. Instead of adding a new parameter, I'm just changing the label, so for name. Now what I'll do inside of here is I'll actually change the print so we can see the distinction. Now when I call the function, birthday greetings two, we see the list now of what I'm overloading, and then down here, four, there we go, this is the birthday card for Hank. So that's overloading a function with the different ways of doing it. I do want to show you how you can overload a function with a different return type. So all of these right now, we're not really returning any values, we're just printing a string, but let's do that with two new functions. This function is temperature check, and from this I will return a string. That's the temperature check, depending where I will stop. Now I will make a new function, but in this time I'm going to return an int. So two functions, overloading them, but depending on the return type. Now if I call this function, see right here, temperature check, and then it says plus one more, press the right arrow to see more. So if I press the right arrow, I see temperature check with the string return in the int. If I select the string return, I just get the temperature check, and if I do it again, and if I select the int return, they're identical. So how do I use one, and if it's a different term type, what do I do? Well, we have to be very specific in how we want to use this, and the way we do this is of course, just set up a new constant here. So tap message is equal to temperature check, but it's still ambiguous and I'll still have an error. What I have to do is be very specific on the type, what I just set up. So in this case, I'll have type of string. Now when I run this, the value is it's hot right here. I go ahead and print this so we can really see it. If I come back in here and change this type to int, the value will change to 99, and that's how I make the distinction between these two overloads out of the return type. This is a little tricky, you have to be very careful about in your code what you're doing so you can be very explicit both to yourself and to your code about what you want. But that's how we do the overloading. Thanks for watching. Stay tuned for the next lesson where I'll show you how to use variadic functions.

Learn on the Go

Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.