Swift's functions

Source: Internet
Author: User

There are two functions defined here, the second has a parameter, and there is a return value, the return value is separated by a, the first one is a normal function, no parameter return value.

5 ; func test () {    print ("asdfa");}
Call
,Int { return1}
Call
Test1 (5)

There are constants, functions with no fixed parameters, variable parameters ... Give default value directly = a value

// 1. With default values, the parameters are all in the back (parameters are not variable) // 2. Call the parameter order as much as possible in the order in which the function is declared func Add (initnum:int = 0,numbers:int ...) -Int {    var totals = initnum    for in  Numbers {        + =    num    }return  totals}


parameter is a variable, parameter is a fairly pointer, reference parameter

// by default, its arguments are constants var a:int)-int{        //return (+ +a)}var6 = inout-int{   //a++ return     a}vtest2 (&m) m


A function is a first-class citizen, which means that it can be returned as another value. This returns the return value of the function in the function.

Func makeincrementer (),return1 +return  var increment = makeincrementer () increment (7)

Functions can also be passed as arguments to another function.

Here we will lessthantenchua this function into the function hasanymatches find the number less than 10

Func hasanymatches (list:int[], Condition:int-Bool)Bool { forIteminchList {ifcondition (item) {return true } } return false} func Lessthanten (number:int)-Bool {returnNumber <Ten } varnumbers = [ -, +,7, A] Hasanymatches (Numbers, Lessthanten)

Swift's functions

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.