Welcome to Swift (Apple's official Swift document translation and annotations 34)---241~247 page (fifth-function)

Source: Internet
Author: User
Tags mathematical functions

In-out Parameters (global parameter)

As the parameter variable described earlier, can only be modified in the function body, if you need the function to modify its parameter values, and you want these changes after the function call is still valid, you can define the use of global parameters.

Define global parameters using the keyword inout, the value of the global parameter is passed at the time of the function call, modified in the function body, and finally the function callbacks the new value to replace the previous value.

Global parameters in a function, only variables can be used as arguments, and constants or literal values cannot be used as arguments. Because constants or literal values cannot be modified. To indicate that the parameter variable can be modified, add a & symbol directly before the variable name.

Attention

Global parameters cannot have default values, and mutable parameters cannot be used as global parameters. If you mark the parameter as InOut, you can no longer use VAR or let to tag them.

The following shows a swaptwoints function, which has two global parameters A and B:

Func swaptwoints (inout a:int, inout b:int) {

Let Temporarya = a

A = b

B = Temporarya

}

This swaptwoints function simply swaps the values of B and a, and functions by storing the value of a in a temporary constant Temporarya, then assigning B to a, and then assigning the Temporarya to B.

You can use two variables of type int to call the Swaptwoints function, which can exchange their values, note that the & symbol is used in front of Someint and Anotherint:

var someint = 3

var anotherint = 107

Swaptwoints (&someint, &anotherint)

println ("Someint is now \ (Someint), and Anotherint are now \ (anotherint)")

Prints "Someint is now 107, and Anotherint are now 3

The above example shows that the initial values of Someint and Anotherint are modified inside the function swaptwoints, even if they are defined outside the function body.

Attention

The global parameter is different from the return value of the function. The Swaptwoints function does not define a return value in the above example, but it still modifies the values of Someint and Anotherint. A global parameter is a workaround that functions can affect outside the body of the function.

function Types (functional type)

Each function will have a specific type, which consists of the parameter type and the return value type.

For example:

Func addtwoints (A:int, b:int), Int {

return a + b

}

Func multiplytwoints (A:int, b:int), Int {

return a * b

}

This sample defines two simple mathematical functions, called addtwoints and multiplytwoints. Each function has two int values, and an int value is returned, and their execution results are similar to mathematical operations.

The type of these two functions is (int, int), int.

This can be interpreted as: "The type of the function is, there are two arguments, they are of type int, and it returns a value of type int"

Here is a function that has no parameters or return values:

Func Printhelloworld () {

println ("Hello, World")

}

The type of this function is (), (). It means "This function has no arguments, it returns void". A function that does not specify a return value will always return void,void in Swfit equal to NULL, writing ().

Using-function Types (use of function types)

Use the type of the function, just as you would with other types in swift. For example, you can define a constant or variable as the type of the function and assign a variable in the corresponding function:

var mathfunction: (int, int), int = addtwoints

This code can be read as:

"Defines a variable called mathfunction, which has a type of function, which takes two int values and returns an int value, and setting this variable requires referencing a function called addtwoints."

The type of the function addtwoints is the same as the variable mathfunction. So this assignment can be checked by Swift's type.

You can now use Mathfunction to invoke the function:

println ("Result: \ (Mathfunction (2, 3)")

Prints "Result:5"

You can use the same method to assign different functions to the same variable, as long as the type of the function matches the variable:

Mathfunction = multiplytwoints

println ("Result: \ (Mathfunction (2, 3)")

Prints "Result:6"

For other types, you can use the Swift automatic function type check when assigning a constant or variable:

Let anothermathfunction = addtwoints

Anothermathfunction is inferred to be of type (int, int), int

function Types as Parameter Types ( functions type as parameter type )

You can use a function type as a parameter type and use it for another function. This allows the function caller to implement part of the function itself when called.

Example:

Func Printmathresult (mathfunction: (int, int), int, a:int, b:int) {

println ("Result: \ (Mathfunction (A, b)" )

}

Printmathresult (Addtwoints, 3, 5)

Prints "Result:8"

This example defines a function called Printmathresult, which has three parameters, the first parameter is called Mathfunction, and its type is (int, int), int, and you can pass any function of this type as the first argument. Second parameter The number and the third parameter are called A and B, and they are all int types, which are used as input values for the math function.

When calling Printmathresult, pass the addtwoints function to it, as well as the integer value 3 and 5, which will call the function (Addtwoints) provided with the 3 and 5来 calls, and output the result 8.

The function of Printmathresult is to output the result of the mathfunction call, regardless of the actual execution of the function, it only focuses on the correct type of the function. This allows Printmathresult to emit some function to the caller of the function, And is in a type-safe manner.

/******************** to be Continued ... ************************/

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.