Parameter defaults and Optional Function Parameters in Swift 3.0

There are more changes in Swift 3.0 to Optional Function parameters, so I thought I should revisit this blog post.

Functions no longer include the name of the first parameter in the function name, and the first parameter is no different to any other, hooray! For info just on function parameters in Swift 3.0, click on my post here.

Swift has an interesting multi-pronged approach to dealing with default and optional function parameters, based in part on its relationship with Objective C and part inspired by other approaches.
It’s worth exploring the options.

Optional function parameters approach 1

First of all, let’s look at one approach to optional parameters:

func output(firstName:String) {
    print(firstName)
}
func output(firstName:String,surname:String) {
    print("\(firstName) \(surname)")
}
output(firstName:"Joe") //prints Joe
output(firstName:"Joe", surname: "Blow") //prints Joe Blow

Wait – there are two functions with the same name? This is called function overloading, where two functions with the same name are differentiated by different parameters (or return values) and can have different implementations.

Parameters with default values approach 1

This technique can also be a way to define default parameters, where the function with less parameters can call the function with extra parameters, defining defaults.

func output(firstName:String) {
    output(firstName:firstName, surname: "Smith")
}
func output(firstName:String,surname:String) {
    print("\(firstName) \(surname)")
}
output(firstName:"Joe") //prints Joe Smith
output(firstName:"Joe", surname: "Blow") //prints Joe Blow

 

Optional function parameters approach 2

As Swift has an Optional variable type, making a parameter Optional, would obviously make it optional 😉 However this doesn’t mean you don’t need to pass anything – you still need to pass nil, and deal with checking the optional contains something in the function.

func output(firstName:String,surname:String?) {
    if let surname = surname {
        print("\(firstName) \(surname)")
    } else {
        print(firstName)
    }

}
output(firstName: "Joe", surname: nil)    //prints Joe
output(firstName: "Joe", surname: "Blow")    //prints Joe Blow

Parameters with default values approach 2

Parameters can be given a default value.Parameters with default values have a few special attributes:

  • to be called in a different order, they need to be declared after any parameters without default values
  • These parameters with defaults can be left out when calling the function
  • If external parameter names are included (the second parameter onwards have external parameter names by default) parameters can be included in any order!

Note: In this example the first parameter specifies an external parameter name so as to be able to called out of order.

func output(firstName:String="Unknown",middleName:String="Unknown",surname:String="Unknown") {
    print("\(firstName) \(middleName) \(surname)")
}
output(firstName: "John", middleName: "Andrew", surname: "Smith")    //prints John Andrew Smith
output(firstName: "John")    //middleName and surname parameters left out! prints John Unknown Unknown
output()        //prints all defaults (Unknown Unknown Unknown)
output(surname: "Smith", middleName: "Julius", firstName: "John")   //parameters out of order! prints John Julius Smith

Optional function parameters approach 3

Which of course makes it possible for an ‘Optional’ variable to have a nil default value. So with a simple tweak to our previous default values example, we can introduce a true optional parameter, using the Optional type.

func output(firstName:String,surname:String?=nil) {
    if let surname = surname {
        print("\(firstName) \(surname)")
    } else {
        print(firstName)
    }

}
output(firstName: "Joe")    //prints Joe
output(firstName: "Joe", surname: "Blow")    //prints Joe Blow

Be careful with naming conflicts mixing these approaches – if you have multiple functions with the same name but different parameters, and one of them has optional parameters, and there’s a possibility that Swift won’t know which function you’re referring to, there’s a naming conflict and the compiler will complain.

Good luck with your Swift functions, hopefully this has been helpful – let us know in comments any other function parameter tips.

For info just on function parameters in Swift 3.0, click on my post here.

For more on functions in Swift, check out my video:

iOS development with Swift - book: https://manning.com/books/ios-development-with-swift video course: https://www.manning.com/livevideo/ios-development-with-swift-lv

Tagged with: , ,
Posted in Swift
One comment on “Parameter defaults and Optional Function Parameters in Swift 3.0

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: