From the course: Learning Higher-Order Functions with Swift

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Passing closures/functions as parameters

Passing closures/functions as parameters

From the course: Learning Higher-Order Functions with Swift

Start my 1-month free trial

Passing closures/functions as parameters

- [Instructor] As I mentioned in the last video, since closures and functions have a type, you can make a parameter that type and pass the closure or function in. So just like a type for a var or a let, you can just a type in your parameter list for a function. And then you pass a closure or function of that type in. So let's look at an example. We had our stringify function that takes a string, an int, and returns a string. So its type is string int return string. Well now we have this function called usesStringFactory and it takes a parameter called stringFunc. Well what's the type of that parameter? It's string and return string. So it needs to be passed in something of that type, string, int, return, string. Do we know something of that type? We sure do. Stringify. So we can pass in stringify as this parameter. And then this function uses stringFactory could call it and return its output. In this case, it's calling it with number and 11. Stringify concatenates those two, passes it…

Contents