Welcome to Swift (Apple's official Swift document translation and annotations 35)---248~253 page (fifth-end of function)

Source: Internet
Author: User

function Types as return Types ( functions type as return value type )

The type of a function can be a return value type for another function. You can write a complete function type after the return value arrow of a function.

For example:

The following example defines two simple functions, Stepforward and Stepbackward. Where the return value of the Stepforward function is more than its input value 1,stepbackward the return value of the function is 1 less than the value it entered. The types of both functions are (Int ), Int:

Func Stepforward (input:int), Int {

return input + 1

}

Func Stepbackward (input:int), Int {

return input-1

}

Here is a function choosestepfunction, whose return type is a function type (INT), The Int. function Choosestepfunction Returns the Stepforward function type or Stepbackward function type according to its parameter of the Boolean type:

Func choosestepfunction (Backwards:bool)-(int)-int {

return backwards? Stepbackward:stepforward

}

Now you can use choosestepfunction to get a function:

var currentvalue = 3

Let Movenearertozero = choosestepfunction (CurrentValue > 0)

Movenearertozero now refers to the Stepbackward () function

The Movenearertozero will be calculated according to the appropriate function until 0:

println ("Counting to Zero:")

Counting to zero:

While CurrentValue! = 0 {

println ("\ (CurrentValue) ... ")

CurrentValue = Movenearertozero (currentvalue)

}

println ("zero!")

3 ...

2 ...

1 ...

Zero!

Nested Functions ( function nesting )

You can define another function in the body of a function, which is called a nested function.

By default, nested functions are unknown to the outside world. But it can be called by the function that encapsulates it. The function that encapsulates it can return it as the return value of a function, which allows nested functions to be used in other scopes.

Code Sample:

Func choosestepfunction (Backwards:bool)-(int)-int {

func Stepforward (input:int), Int {return input + 1}

func Stepbackward (input:int), Int {return input-1}

return backwards? Stepbackward:stepforward

}

var CurrentValue =-4

Let Movenearertozero = choosestepfunction (CurrentValue > 0)

Movenearertozero now refers to the nested Stepforward () function

While CurrentValue! = 0 {

println ("\ (CurrentValue) ... ")

CurrentValue = Movenearertozero (currentvalue)

}

println ("zero!")

-4 ...

-3 ...

-2 ...

-1 ...

Zero!

/******************** the end of this chapter ... ************************/

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.