Programming in Swift: Functions & Types

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

Part 1: Functions

05. Advanced Parameters

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: 04. Overloading Next episode: 06. Challenge: Overloads & Parameters

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: 05. Advanced Parameters

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

Transcript: 05. Advanced Parameters

Now that you’ve got a handle on overloading functions, there are a couple more fancy things you should know how to do with function parameters.

The applications for the techniques in this episode are more rare. But, even if you never use them yourself, chances are you’ll come across code that does. So, let’s take a look at variadic parameters and the inout keyword.

First, variadic parameters. There is a function you already know that uses variadic parameters: print!

print(jessyGrade, ozmaGrade, "meow")

I’ve passed three comma-separated Strings into this function,

  • and when I run the playground…

All three are printed out.

  • If you option click on print to see the documentation…

You’ll see that the first parameter has these three dots after the type. That’s saying the function takes zero or more parameters of that type. This is where the term “variadic” comes from. The number of things passed in for that parameter varies.

We can use the same syntax to create our own function with a variadic parameter. Like a function that returns the highest grade for 0 or more grades passed in.

func getHighestGrade(for grades: Int...) -> Int {

}

This one takes 0 or more Ints and will return an Int. Inside the body of the function, the variadic parameter is treated like an array. So you could do things like loop over it, or call the max method on grades to find the highest one.

  grades.max() ?? 0

max returns an optional, because the array might be empty. I’ve used nil coalescing to return 0 in that case. Now we can call this function with no arguments…

getHighestGrade()

…and it’ll return 0. And we can can pass in one argument, or multiple comma-separated arguments!

getHighestGrade(for: jessyGrade, ozmaGrade)

And our function will return the highest value. What you might think you can do, but you can’t do, is pass in an array…

getHighestGrade(for: ozmaAllGrades)

That will just get you an error.

//getHighestGrade(for: ozmaAllGrades)

The last thing I’ll show you in this episode is the inout keyword.You might remember that when you pass a value into a function, it creates a copy of it, and that copy is essentially a constant. It’s immutable and can’t be changed in the function body.

So if we tried to write a function that take an Int

func incrementAndPrint(_ value: Int) {

}

and then increments and prints that parameter…

  value += 1
  print(value)

We get an error. Normally, this is the behavior you want! Ideally, a function doesn’t alter its parameters.

If it did, then you couldn’t be sure of the parameters’ values and you might make incorrect assumptions in your code, leading to the wrong data.

But sometimes, you do actually want a function to change a parameter directly. That kind of behavior is called copy-in copy-out.

To use it in your own functions, use the inout keyword before the type of the parameter you want to change.

func incrementAndPrint(_ value: 😺inout🛑 Int) {...

Now, you have something like a local variable, instead of a constant. The function can mutate the parameter, and the changed value will be copied out when the function returns.

To call a function with an inout parameter, you need to add an ampersand in front of the argument

incrementAndPrint(&count)

Now, each time you call this function…

incrementAndPrint(&count)
incrementAndPrint(&count)
count

that count variable will be incremented!