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