function

Optional parameters

func getFirstInitial(from name: String?) -> String? {
  return name?.first
}

Functions can accept optional types and return optional types. When a function cannot return a reasonable instance of the requested type, it should return nil

variable parameter

func totalStudent(data: String...) -> Int {
  let numStudents = data.count
  return numStudents
}
print(totalStudent(data: "Denial", "Peter"))
// print: 2

Input and output parameters

var currentSeason = "Winter"
func season(month: Int, name: inout String) {
  switch month {
    case 1...2:
      name = "Winter ⛄️"
    case 3...6:
      name = "Spring 🌱"
    case 7...9:
      name = "Summer ⛱"
    case 10...11:
      name = "Autumn 🍂"
    default:
      name = "Unknown"
  }
}
season(month: 4, name: &currentSeason)
print(currentSeason) // Spring 🌱

Default parameters

func greet(person: String = "guest") {
  print("Hello \(person)")
}
greet() // Hello guest
greet(person: "Aliya") // Hello Aliya

Implicit return

func nextTotalSolarEclipse() -> String {
  "April 8th, 2024 🌎"
}
print(nextTotalSolarEclipse())
// print: April 8th, 2024 🌎

Parameters & Arguments

func findSquarePerimet(side: Int) -> Int {
  return side \*4
}
let perimeter = findSquarePerimet(side: 5)
print(perimeter) // print: 20
// Parameter: side
// Argument: 5

return multiple values

func smartphoneModel() -> (name: String, version: String, yearReleased: Int) {
  return ("iPhone", "8 Plus", 2017)
}
let phone = smartphoneModel()
print(phone.name)         // print: iPhone
print(phone.version)      // print: 8 Plus
print(phone.yearReleased) // print: 2017

Omit parameter labels

func findDiff(\_ a: Int, b: Int) -> Int {
  return a -b
}
print(findDiff(6, b: 4)) // prints: 2

Multiple parameters

func convertFracToDec(numerator: Double, denominator: Double) -> Double {
  return numerator / denominator
}
let decimal = convertFracToDec(numerator: 1.0, denominator: 2.0)
print(decimal) // prints: 0.5

return value

let birthYear = 1994
var currentYear = 2020
func findAge() -> Int {
  return currentYear-birthYear
}
print(findAge()) // prints: 26

Call functions

func greetLearner() {
 print("Welcome to QuickRef.ME!")
}
// function call:
greetLearner()
// print: Welcome to QuickRef.ME!

Basic functions

func washCar() -> Void {
  print("Soap")
  print("Scrub")
  print("Rinse")
  print("Dry")
}
Comments