Swift's initial function (ii)

Source: Internet
Author: User

The previous article describes the function's default parameters and input and output functions, today to learn something else.

1. Multiple return value functions:

I don't know if you remember the concept of Ganso. No, a return value of a function return value type is a particular type, and multiple return value of the function return value type is a meta-ancestor type.

Look at the following code:

<pre name= "code" class= "OBJC" >func multireturnfunc (s1:string, name s2:string) (newstring:string, Cou Nt:int) {    var str = s1 + s2    return (str, countelements (str))}println (Multireturnfunc ("Hello", Name: "Weasley"))

Output:

(Helloweasley, 12)

The above code return value is a string and the length of the string, the return value of the function Multireturnfunc is more than one, the type is Ganso.


2. Variable parameter function

Sometimes when you are unsure of the number of input values, you need to use the variable parameter function, which can be inserted after the type name of the formal parameter three dot symbol (...). To write a deformable parameter. Like what:

Func Add (Array:int ...)-int{    var sum = 0 for    i        in array{sum + = i    }    return Sum}println (Add (1, 2, 3,4))

At this point the output is 10, this add function is a variable parameter function, we randomly passed a few parameters can be. But one thing to be aware of is that ... Must be the last parameter.


3. Function Type:

In Swift, you can use the function type as you would any other type, you can define a variable or constant as a function type, and assign a corresponding function to the variable.

var Callfunc: (A:int, b:int), Int = add

This code can be understood as: Defines a callfunc function, the input parameter is 2 int value, the output is an int value, the function internal operation calls the internal operation of the add function.

Functions can be passed to another function as one line parameter:

Func Add (A:int, b:int), Int {    return a + B}func minus (A:int, b:int), int{    return A-b}var CA Llfunc: (int, int), int = Addcallfunc = Minusfunc Another (funcparameter: (int, int), int, i:int, j:in T)-int{    return Funcparameter (i,j)}println (Another (Callfunc, 3, 5))


The first argument (Funcparameter) of the Another function is a function at which the output is-2.
When we call the another function again, the first argument passes the Callfunc function, and the CALLFUNC function is the internal operation of the add, so the result is-2.

Similarly, since a function can be passed as a parameter to a function, the function can also be used as the return value of a function:

Func Max (A:int, B:int), int{    return a > B? a:b}func min (A:int, b:int), int{    return A & Gt B? B:a}func Choosefunc (#getMax: Bool) (int, int), int {    return getmax? Max:min}var MyFunc:( int, int), int = Choosefunc (getmax:true) println (MyFunc (2,3))


A max function and a min function are defined first, then a choosefunc function is defined, the return value of the Choosefunc function is a function type, the input value is a bool type, and the Max function is selected when the input is true. When False, select the Min function.


4. Nested functions:

Functions can be nested inside a function, not visible to the outside world, such as the above code can be modified to:

Func Choosefunc (#getMax: Bool) (int, int), int {    func max (A:int, B:int), int{        return a &G T B? A:b    }    func min (a:int, b:int), int{        return a > B? b:a    }    return Getmax? max:min}va R MyFunc:( int, int), int = Choosefunc (getmax:false) println (MyFunc (2,3))

The effect is just the same.


You are welcome to discuss together.






Swift's initial function (ii)

Related Article

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.