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
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
func totalStudent(data: String...) -> Int {
let numStudents = data.count
return numStudents
}
print(totalStudent(data: "Denial", "Peter"))
// print: 2
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: ¤tSeason)
print(currentSeason) // Spring ð±
func greet(person: String = "guest") {
print("Hello \(person)")
}
greet() // Hello guest
greet(person: "Aliya") // Hello Aliya
func nextTotalSolarEclipse() -> String {
"April 8th, 2024 ð"
}
print(nextTotalSolarEclipse())
// print: April 8th, 2024 ð
func findSquarePerimet(side: Int) -> Int {
return side \*4
}
let perimeter = findSquarePerimet(side: 5)
print(perimeter) // print: 20
// Parameter: side
// Argument: 5
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
func findDiff(\_ a: Int, b: Int) -> Int {
return a -b
}
print(findDiff(6, b: 4)) // prints: 2
func convertFracToDec(numerator: Double, denominator: Double) -> Double {
return numerator / denominator
}
let decimal = convertFracToDec(numerator: 1.0, denominator: 2.0)
print(decimal) // prints: 0.5
let birthYear = 1994
var currentYear = 2020
func findAge() -> Int {
return currentYear-birthYear
}
print(findAge()) // prints: 26
func greetLearner() {
print("Welcome to QuickRef.ME!")
}
// function call:
greetLearner()
// print: Welcome to QuickRef.ME!
func washCar() -> Void {
print("Soap")
print("Scrub")
print("Rinse")
print("Dry")
}