"Swift" Learning Notes (vi)--functions

Source: Internet
Author: User

This is the basic function of a programming language, and any language has the concept of function, which is a separate block of code that accomplishes a particular task.


How the function is created:

1. Create a function without parameter return value (virtually all functions have a return value, this function returns void, which is an empty tuple)

Func TestFunc () {    }

2. Create a function with a return value

Func TestFunc (), string{    return "Hello"}

3. Create a function with parameters and return values

Func TestFunc (name:string), string{    return "Hello" +name}

4. Create a function with multiple parameters and multiple return values

Func TestFunc (Name:string,age:int)-(myname:string,myage:int) {    return (name,age)}

5. Create a function with a parameter name

Func testFunc (MyName name:string,myage age:int) (myname:string,myage:int) {    return (name,age)}

PS: Call

TestFunc (MyName:" Xiao Wang ", MyAge:)


6, the 5th in another way, is the external parameter name and local variable name is the same time.

Func TestFunc (#name: String, #age: Int) (myname:string,myage:int) {    return (name,age)}testfunc (name: "Xiao Wang", age : 18)

7. Create a function with a default value parameter

Func TestFunc (#name: String,age:int = ten) (myname:string,myage:int) {    return (name,age)}testfunc (name: "Xiao Wang", AGE:18)

PS: Parameters with default values either do not specify an external parameter name (Swift will specify one by default is equivalent to a # number) or must be specified, cannot use the # number


8. Create a variable parameter function

Func allnums (nums:double ...), double{    var totalnum:double = 0 for    num in nums{        totalnum + = num    }
   return Totalnum}allnums (a)

9, function parameters are constants, can not be modified OH

For example:

Func error (A:int) {    A = 2    this will cause an error: Cannot assign to ' let ' value ' a '}

That can be created to modify. Of course. Add the var keyword.

Func error (Var a:int) {    a = 2}error (3)


10, the function parameter is the value of the transfer, that there is no way to make it a reference to pass it, that is, the parameters have been modified by the function, the external value of the function has changed


Func yy (inout a:int,inout b:int) {    var c = a    a = b    b = A}yy (&10, &20)

But this is wrong and what is wrong. That is, when the function is called, the incoming literal is changed. So to pass in the variable

var a = 10,b=20yy (&a, &b)


Big Brain storm: The function actually has the type, it also can be used as the parameter, the type.

For example:

Func yy (inout A:int,inout B:int)

This function, whose type is (Int,int), is ().

It can and int double this kind of use OH. ----'s so powerful.









Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"Swift" Learning Notes (vi)--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.