Welcome to Swift (Apple's official Swift document translation and annotations 30)---225~230 page (fifth-function)

Source: Internet
Author: User

Functions (function)

A function is a piece of code that performs a specific task. Identify and invoke them by name.

In swift, each function has a type, including the parameter type of the function and the type of the return value. These types are used in the same way as other types in Swift, which allows functions to be passed as arguments to another function, and functions can be returned from one function, and a function can also be written inside another function. This allows for more efficient encapsulation and nesting.

Defining and calling Functions ( defining and calling functions )

When you define a function, you can optionally name one or more parameter type values, and the return value type after the function executes.

Each function has a name, which is the function that describes the function. If you want to call this function, you can directly use the name of the function and pass the corresponding argument (the type of the parameter should match the argument list of the function).

The following is a simple example of a greetingforperson function that takes a person name as a parameter and returns a greeting (a string type value):

Func SayHello (personname:string), String {

Let greeting = "Hello," + PersonName + "!"

return Greeting

}

The above code contains information about the function definition: the keyword func for the prefix, and the return arrow symbol immediately following the function name, which represents the return value type of the function.

This definition example describes what this function does, what it receives, and what it returns when the function is completed. This makes it very clear that the function will be called elsewhere in the code:

println (SayHello ("Anna"))

Prints "Hello, anna!"

println (SayHello ("Brian"))

Prints "Hello, brian!"

You can call the SayHello function multiple times and use a different parameter value. To simplify the function body, you can use a single line of code consolidation:

Func Sayhelloagain (personname:string), String {

return "Hello again," + PersonName + "!"

}

println (Sayhelloagain ("Anna"))

Prints "Hello Again, anna!"

function Parameters and return values ( parameter and return value of functions )

In Swift, the parameters and return values of the function are very resilient. You can define any type of return value, or you can use one or more parameters (or anonymous parameters).

multiple Input Parameters ( multiple parameters )

Functions can have multiple parameters, which are written in parentheses within the function and separated by commas.

code example:

Func halfopenrangelength (Start:int, end:int), Int {

return End-start

}

println (Halfopenrangelength (1, 10))

Prints "9"

Functions without Parameters ( no parameter function )

The function does not require a parameter to be used. The following function example uses a function with no parameters, and when it is called, it always returns the same string information:

Func SayHelloWorld (), String {

return "Hello, World"

}

println (SayHelloWorld ())

Prints "Hello, World"

There is still a need to use parentheses () after the function name when the function definition is undefined.

Functions without return values ( no return function )

The function can also not define the return value, so the code sample defines a function Wavegoodbye, which simply prints the output string value instead of returning this value:

Func Saygoodbye (personname:string) {

println ("Goodbye, \ (personname)!")

}

Saygoodbye ("Dave")

Prints "Goodbye, dave!"

Since the function does not need to return a value, it is not used when defining the function return shears (-)

Note the point:

Strictly speaking, the Saygoodbye function still has a return value, although it does not define its return value. When the function does not have a return value defined, the function returns a value of type void. It is an empty tuple, with a zero element number and can be written ().

The return value of the function can also be ignored when calling the function:

Func Printandcount (stringtoprint:string), Int {

println (stringtoprint)

return countelements (stringtoprint)

}

Func printwithoutcounting (stringtoprint:string) {

Printandcount (Stringtoprint)

}

Printandcount ("Hello, World")

Prints "Hello, World" and returns a value of 12

Printwithoutcounting ("Hello, World")

Prints "Hello, world" but does not return a value "

Note the point:

The return value of the function can be ignored, but the function must have a return value. Defines the return value of the function, but if the function does not return a value at the end, a compilation error will result.

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.