5.3 Function Types
We're not done with functions yet, because there is another topic that is crucial to understand in the Swift language: function types. In Swift, functions are first-class citizens, meaning you can actually create instances of them and store them as variables. And if you can do that, what is the type of the variable that has been created? Let's take a look.
1.Introduction3 lessons, 15:05
2.Foundational Concepts5 lessons, 43:51
3.Working With Collections3 lessons, 26:26
4.Controlling Flow7 lessons, 1:22:24
5.Functions5 lessons, 51:54
6.Creating Types6 lessons, 1:08:10
7.Conclusion1 lesson, 01:15
5.3 Function Types
Functions are a very interesting thing in the world of Swift. And one thing that makes them interesting with respect to some other languages, maybe not so much amongst others, is that within the world of Swift, functions are first class citizens. Which means they're actually types in and of themselves. Which means we can create variables of a function type which sounds a little strange. So let's go ahead and take a look at what exactly that means. So every single function that you write has a type, a function type, which is ultimately defined as the input parameters and the return type of that function. So let's see this as an example. So if I were to create a function let's say, and we'll call this my function, and it took in a couple of parameters. And we'll say that we have a name, which is going to be a string, and an age, which is going to be an integer, and it's not going to return anything. So we're going to go ahead and have some sort of function body, and then they'll be some sort of code in here to do some sort of functionality. Now this particular function has a type, a function type, and as I said before the function types are specified by the input and output of a particular function. So the way that we talk about this is we use the open and close parentheses that are going to specify the parameter types. So in this case we have a string and we have an Int and then we have an output. In this case we have an output of nothing. So this right here is considered a type. Because we're specifying the parameter here or the list of parameters as well as the output. Now if we had another function who didn't have any inputs then it could look something like this. So that's the basic structure of what a function type looks like. So how Can we use this function type? Well, we can start off by saying let's create a variable, and we'll call this function, and we're going to set this equal to my function. And not only are we going to set this equal to my function, but we're not going to pass in any parameters. So by doing this what we're ultimately doing is we're creating a variable that contains the function definition as well as basically a pointer to this function, that I can then come down and use as a function. And I can call this function, and as you see here it has a type, a function type of string. And INT has input values and no output value so I could call this Variable, just like any other function that I normally would. So why would you do something like this? Well it's a very interesting question, but ultimately what this allows us to do is define some sort of function that has some sort of reusable functionality. And I can use that to pass it into another function to maybe execute some sort of logic. Or I can even generate a function or a function type and use it as a return value from a function. And like any other variables or constants within the Swift programming language, this is using implicit typing. So what I could ultimately be saying here is that my function is at, my variable is actually a function whose function type looks like this. So this is another valid statement here because now we're being explicit about the type of our variable. Just like we've done in the past. So let's go ahead and go back to another example that we were using before that will call say hello. And we're going to keep the simple for now are going to specify a name. And it's going to be of type String. And it is going to return a string. And we're simply going to return hello. And then the person's name that we pass in. Just like we've seen before, nothing too crazy here. So now what I want to do is I'm going to create a variable that's going to be called greet. And we're going to set that equal to our say hello function. So now I have a variable called greet that I can use just as I would use any other function. So let's say all I wanted to do now is print the results. So now, I could go ahead and call this just like I would any other function and I could say Anna. I want to greet Anna and we'll go ahead and save that. And now I could run this and it's going to run just like any other function that I've used before. It's going to say, hello Anna. So I've created this variable that encapsulates this functionality that I've done here. Now let's say I wanted to create some sort of custom function that's going to take in this greet as a parameter, because that's absolutely possible. So the way that we specify a function that has an input type as a function type, is just like we've done anything else so far. We can say function, and we'll call this use function. And now we're going to specify a name so we'll call this function. And then we can specify its type and its type as I mentioned before is the function type that of the function that we want to take in. so in this case, we're going to take in a function that has a parameter of a string. And that has an output of a string. And then we can have some sort of functionality internally that's simply going to execute that function by calling that particular function with some sort of input, in this case we'll say Derek. Just like that, so we can now save this. And now if I wanted to pass my function into my use function here, so all I would have to do a specify my use function method here or my use function function, which is going to take in a function type. And at this point all I have to do is pass in greet. So now I can call save. And one additional thing so you can see an output that I'm going to do here. Is because this function returns a string, but my exterior function doesn't return anything, that means that there's no value that's going to be returned from use function. But the interior function is going to return something. I can go ahead and then print the results of this operation by simply specifying print like this. And now I can save. Then I can run my application. And it's going to say Hello Derek because I have created my say hello function that has a string input and string return function type. I have a use function that's going to take in a function that has that particular type, then I create a variable that's going to store my say hello, which has that function type. And then I'm gonna call use function. That's a little bit of a contrived example. But as you can see here it's going to work. Now we can do the same thing for return values. I could say function return function. And I could say it's going to take in nothing but then the return type here is going to be just similar as I had before, where we take an input of a string and we have an output of a string. And then from within here I could simply return say hello. And that will work just the same way. So as you can see now functions within the world of Swift are definitely first class citizens and that we can use them as types to create variables, to pass as inputs into functions and to get as output functions. So as you can see these are very, very powerful things. Now, it's important to understand the concept a function types, because when we discuss the next topic, it's gonna become very important that this is kind of ingrained into your head. And that next topic is closures.