Swift functions with external parameters

Source: Internet
Author: User

Detailed definition

Function Parameter Names

Function parameters has both an external parameter name and a local parameter name. An external parameter name was 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 it external name, and the second and subsequent parameters use their local name as Their external name. All parameters must has a unique local names, but could share external parameter in common.

Specifying External Parameter Names

You write a 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 the external parameter name for a parameter, which external name must always being used when your call T he function.

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

    1. func sayhello ( to person: string, and Anotherperson: string), string {
    2. return "Hello \" Span class= "VC" >person) and \ (anotherperson) ! "
    3. }
    4. print (sayhello (to: "Bill", and: Span class= "s" > "Ted"))
    5. //prints "Hello Bill and ted!"

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

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

Omitting External Parameter Names

If you don't want to use an external name for the second or subsequent parameters of a function, write an underscore ( ) instead of a 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 a underscore is extraneous.

Default Parameter Values

You can define a default value of parameter in a function by assigning a value to the parameter after that P Arameter ' s type. If A default value is defined, you can omit this 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-all calls to the function with 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.