Swift functions with external parameters

详细定义

Function Parameter Names

Function parameters have both an external parameter name and a local parameter name. An external parameter name is used to label arguments passed to a function call. A local parameter name is used in the implementation of the function.

  1. func someFunction(firstParameterName: Int, secondParameterName: Int) {
  2. // function body goes here
  3. // firstParameterName and secondParameterName refer to
  4. // the argument values for the first and second parameters
  5. }
  6. someFunction(1, secondParameterName: 2)

By default, the first parameter omits its external name, and the second and subsequent parameters use their local name as their external name. All parameters must have unique local names, but may share external parameter in common.

 

Specifying External Parameter Names

You write an external parameter name before the local parameter name it supports, separated by a space:

  1. func someFunction(externalParameterName localParameterName: Int) {
  2. // function body goes here, and can use localParameterName
  3. // to refer to the argument value for that parameter
  4. }

NOTE

If you provide an external parameter name for a parameter, that external name must always be used when you call the function.

Here’s a version of the sayHello(_:) function that takes the names of two people and returns a greeting for both of them:

  1. func sayHello(to person: String, and anotherPerson: String) -> String {
  2. return "Hello \(person) and \(anotherPerson)!"
  3. }
  4. print(sayHello(to: "Bill", and: "Ted"))
  5. // prints "Hello Bill and Ted!"

By specifying external parameter names for both parameters, both the first and second arguments to thesayHello(to:and:) function must be labeled when you call it.

The use of external parameter names can allow a function to be called in an expressive, sentence-like manner, while still providing a function body that is readable and clear in intent.

Omitting External Parameter Names

If you do not want to use an external name for the second or subsequent parameters of a function, write an underscore (_) instead of an explicit external name for that parameter.

  1. func someFunction(firstParameterName: Int, _ secondParameterName: Int) {
  2. // function body goes here
  3. // firstParameterName and secondParameterName refer to
  4. // the argument values for the first and second parameters
  5. }
  6. someFunction(1, 2)

NOTE

Because the first parameter omits its external parameter name by default, explicitly writing an underscore is extraneous.

Default Parameter Values

You can define a default value for any parameter in a function by assigning a value to the parameter after that parameter’s type. If a default value is defined, you can omit that parameter when calling the function.

  1. func someFunction(parameterWithDefault: Int = 12) {
  2. // function body goes here
  3. // if no arguments are passed to the function call,
  4. // value of parameterWithDefault is 42
  5. }
  6. someFunction(6) // parameterWithDefault is 6
  7. someFunction() // parameterWithDefault is 12

NOTE

Place parameters with default values at the end of a function’s parameter list. This ensures that all calls to the function use the same order for their nondefault arguments, and makes it clear that the same function is being called in each case.

 

posted @ 2015-07-01 06:50  uu2008  阅读(218)  评论(0编辑  收藏  举报