Swift 1.1 language 7th function and closure

Source: Internet
Author: User

Swift 1.1 language 7th function and closure

In programming, as the processing problem becomes more and more complex, the amount of code increases rapidly. Where a large number of code tends to repeat or approximate each other. The code will be difficult to maintain if it is not addressed in an effective manner. In order to solve this problem, the concept of function has been put forward. Functions can be used to encapsulate the code of a particular function and then be used in many places. This chapter will explain the functions and closures of the relevant content. This article is selected from theSwift 1.1 language Quick Start

7.1 Introduction to Functions

A function is a block of code that performs a specific task. The use of functions can bring a lot of benefits to development. Two points are summarized below.

1. The structure is clear and easy to understand

If the code in a program is very long and the functionality of the implementation is not the same, you can extract the code snippet for each function to use as a function. This makes the program structure clear and easy to understand. The following figure 7.1 is the best implementation.


Figure 7.1 function using 1

2. Reduce the amount of code written and time

In a program, a code snippet that has the same functionality can be presented as a function. In this way, you can reduce the amount of code written, which shortens development time. The following figure 7.2 is the best embodiment. This article is selected from theSwift 1.1 language Quick Start


Figure 7.2 Using the function 2

3. Form of function

A complete function is composed of the Func keyword, the function name, the parameter table, and the return value type of the function, as shown in form 7.3.


Figure 7.3 form of the function

According to the parameter table, the function can be divided into the parameterless function and the parametric function. The following is a brief explanation of the parameterless function and the parameter function. This article is selected from theSwift 1.1 language Quick Start

Swift 1.1 language 7th function and closure

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.