One convenience in Swift is the ability to specify default values for parameters. These allow you to omit the parameter when calling, as the default value will be provided instead. Let's use the same example as earlier in this recipe, where we are creating a contact app to hold information about our family and friends. Many of your family members are likely to have the same family name as you, so we can set the family name as the default value for that parameter. Therefore, the family name only needs to be provided if it is different from the default.
Enter the following code into a playground:
func fullName(givenName: String,
middleName: String,
familyName: String = "Moon") -> String {
return "\(givenName) \(middleName) \(familyName)"
}
Defining a default value looks similar to assigning a value to the familyName: String = "Moon" parameter. When calling the function, the...