Swift functions with external parameters

來源:互聯網
上載者:User

標籤:

詳細定義

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.

 

Swift functions with external parameters

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.