Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 5: Functions & Named Types

36. Functions & Return

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: 35. Introduction to Functions Next episode: 37. 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: 36. Functions & Return

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

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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)