Programming in Swift: Functions & Types

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

Part 1: Functions

02. Review Functions

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: 01. Introduction Next episode: 03. Challenge: Functions

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: 02. Review Functions

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

Transcript: 02. Review Functions

You’ve had a fair bit of experience writing functions already! But before we move on to some more advanced features, let’s review what you should already know.

This is a function declaration! Each one starts with the ❤️func keyword, and must have a 💛name, 💚parameter list, and 🧡body.

❤️func❤️ 💛printMeow💛💚()💚 🧡{
  print("Meow!")
}🧡

Parameter lists can be empty, like the one for this printMeow function.

func printMeow💛()💛 {
  print("Meow!")
}

When there are no parameters, you can just call the function with an empty pair of parentheses after the function name.

printMeow()

Parameter lists can also contain one or more parameters, like this printPassStatus function:

func printPassStatus💛(grade: Int)💛 {
  print(grade >= passingGrade ? "You passed!" : "Glue != food.")
}

When you call a function with parameters, you need to provide values as arguments for those parameters.

printPassStatus(grade: jessyGrade)

Remember, Swift is a “type safe language”! The argument type must match the parameter type. You can provide default values for parameters when you expect a specific value to be used most of the time.

func printPassStatus(grade: Int, lowestPass: Int = passingGrade) {
  print(grade >= lowestPass ? "Good kitty!" : "Keep your paws off the table!")
}

Parameters with default values should usually be near the end of the parameter list. When you call a function like this, you don’t need to provide arguments for parameters with a default value.

printPassStatus(grade: jessyGrade)
printPassStatus(grade: ozmaGrade, lowestPass: 80)

By default, parameter names are used at the function’s call site as argument labels.

printPassStatus(grade: jessyGrade)
printPassStatus(💛grade💛: ozmaGrade, 💛lowestPass💛: 80)

But you can assign argument labels that differ from their parameter names! Argument labels go before parameter names in the parameter list.

func printPassStatus(for grade: Int, lowestPass: Int = passingGrade) {
  print(grade >= lowestPass ? "You've earned a treat." : "🙃")
}

Then, when you call the function, you’ll use the specified argument label instead of the parameter name:

printPassStatus(for: ozmaGrade, lowestPass: 80)

You can also choose to have no argument labels by using underscores in their place.

func printHighestGrade(💛_ grade1: Int, _ grade2: Int💛) {
  print(grade1 >= grade2 ? grade1 : grade2)
}

At the call site, you simply pass in the arguments, separated by commas.

printHighestGrade(jessyGrade, ozmaGrade)

Functions can also return values. The return type is specified after the parameter list and a return token. This function will return a boolean!

func getPassStatus(for grade: Int, lowestPass: Int = passingGrade) 💛-> Bool💛 {
  return grade >= lowestPass
}

Then in the function body, use the return keyword followed by the value you want to return

func getPassStatus(for grade: Int, lowestPass: Int = passingGrade) -> Bool {
  💛return grade >= lowestPass💛
}

When a function returns something, that value can be stored in a variable or constant.

let jessyPassStatus = getPassStatus(for: jessyGrade)

If the body of a function only contains one statement, it can implicitly return the result of that statement without the return keyword

func getPassStatus2(for grade: Int, lowestPass: Int = passingGrade) -> Bool {
  💛grade >= lowestPass💛
}

And the function works exactly the same.

let ozmaPassStatus = getPassStatus2(for: ozmaGrade, lowestPass: 80)