funcgreet(name:String="Guest") {print("Hello, \(name)")
}greet()greet(name:"Emily")
If no name is provided, Swift uses "Guest".
enumPasswordError:Error {casetooShort
}
funccheckPassword(_password:String)throws->String {ifpassword.count<6 {throwPasswordError.tooShort
}return"Password is valid"
}
do {letresult=trycheckPassword("123")print(result)
}catch {print("The password is too short.")
}
Remember:
throws: The function might fail.throw: Create the error.try: Attempt to run the function.catch: Decide what to do if it fails.A closure is similar to a function without a name. It can be stored in a variable or passed into another function.