O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Defining a parameter's default values

We can define default values for any parameter by using the equals to operator (=) within the function definition when we declare the parameters. The following example shows how to declare a function with a parameter's default values:

func sayHello(name: String, greeting: String = "Bonjour") {  
  print("\(greeting) \(name)") 
} 

In the function declaration, we have defined one parameter without a default value (name:String) and one parameter with a default value (greeting: String = "Bonjour"). When a parameter has a default value declared, we are able to call the function with or without setting a value for that parameter. The following example shows how to call the sayHello() function without setting the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required