Programming in Swift: Fundamentals

Oct 19 2021 Swift 5.5, iOS 15, Xcode 13

Part 5: Functions & Named Types

36. Functions & Return

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions

See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 35. Introduction to Functions Next episode: 37. Challenge: Functions

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

If you followed along and built the Bullseye app, you might remember writing functions like amountOff or sliderValueRounded. Both of those methods returned a value. When a function or method returns a value, once the function is called that value can be stored or used in-place.

func amountOff() -> Int {
  abs(target - sliderValueRounded())
}
  
func pointsForCurrentRound() -> Int {
  let maximumScore = 100
  let difference = amountOff()
  let bonus: Int
    if difference == 0 {
      bonus = 100
    } else if difference == 1 {....
func sliderValueRounded() -> Int {
  Int(sliderValue.rounded())
}

func amountOff() -> Int {
  abs(target - sliderValueRounded())
}
...) -> {...
...) -> Bool {... 
  return grade >= lowestPass
grade >= lowestPass
func getPassStatus
let chrisPassStatus = getPassStatus(for: chris.grade)
let samPassStatus = getPassStatus(for: sam.grade)
let classPassStatus = getPassStatus(for: chris.grade) && getPassStatus(for: sam.grade)
let sam = (name: "Sam", grade: 99, pet: nil)
let sam: (name: String, grade: Int, pet: String?) = (name: "Sam", grade: 99, pet: nil)
typealias Student =
let chris: Student = (name: "Chris", grade: 49, pet: "Mango")
let sam: Student = (name: "Sam", grade: 99, pet: nil)
func orderPetCollar(for student: Student) {

}
guard let pet = student.pet
guard let pet = student.pet else { }
guard let pet = student.pet else { return }
print("One custom collar for \(student.name)'s pet, \(pet)!")
orderPetCollar(for: chris)
orderPetCollar(for: sam)