Syntax that differs from C + + and Java in the Swift language (v)

Source: Internet
Author: User

This section will refer to other applications for functions in Swift, including the following:

Default parameter values and variable parameter values, constant parameters, return function types, and function nesting:

First, default parameter values and variable parameter values

The default parameter value is defined as the initial value of the parameter in the declaration of the function, such as:

Func Sayhelloto (name:string,greeting:string = "Hi", punctuation:string = "! {} In the call can be: Sayhelloto (name: "PlayGround", punctuation: "!!!"). ", Greeting:" Hi ")

Because parameters with default parameter values can be swapped sequentially for functions with default parameters, it is not recommended. The print function is the function that has the delimiter and terminator initial values.

For functions with variable-length parameter types,

Func mean (numbers:double ... ), Double {    for in  numbers {        sum+ = number    }      The return sum/Double (numbers.count)} Call can be: Mean (2) or mean (2,  3,4,5,6)

However, a function can have only a variable length parameter.

Two, constant parameters

Unlike Java and C + +, Swift cannot change the value of a parameter in a function because the type declaration of the value of the passed parameter is let, and a good function avoids the need to change the value of the function argument.

But if you want to change the value of a parameter, you need to declare it:

Func Tobinary (Var num:int), String {    return num/2}

Declare the type as VAR,

But this change is only within the function, but it is not changed outside of the function,

This is because this method is passed in by value, not by reference, but by copy inside the function.

If you need a bash function to change the value of an external parameter, you need to add inout

func swapint (inout a:int, inout b:int) {    = (b,a)}
var x:int = 1
var y:int = 2
Swapint (&X,&B)

Third, return function type

This is also very simple, the return type is the function is good:

Func tire1 (weight:int), (int), int {    return weight> Ten ? tire1 (weight): Tire2 (weight)}

Iv. Nesting of functions

Func Feebyprice (price:int,weight:int),Int {
Func Choose (weight:int) (int), int {    return weight>10? tire1 (weight): Tire2 (weight)}
Return ...
}

Syntax that differs from C + + and Java in the Swift language (v)

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.