1. Definition of function and concept of calling function
A function is a block of code that accomplishes a particular task, giving the code block a meaningful name, called the function name. When the function needs to be executed, the name can be called to execute the function. can help the program modularization, reduce the code redundancy, help the programmer to better understand the program.
Classification of functions
(1) Other programmers have encapsulated functions. such as the print function:
println("Hello Swift")
(2) A function that the programmer encapsulates itself.
Syntax for swift function definition and invocation
Note the difference between swift and other language functions:
The first function is to use the Func keyword.
Second, the function of the other language first write the parameter type followed by a space symbol and then write the parameter name, and Swift is the opposite of first write the function's parameter name followed by a colon symbol and then write the parameter type.
Again, the return value for other languages is preceded by a space symbol in front of the function name, and Swift is written behind the parameter list with the arrow-----and the return type.
Note Swift and Java have just one file and no need to write a function declaration.
function definition:
Func function name (parameter name 1: parameter type, parameter name 2: Parameter type ...) The return value type of the function, {
???? function body
???? return value
}
Call to function:
Function name (argument list)
An example defines a function that uses each element of an output array.
func showIntergerArray(array:[Int]){ forinarray{ print("\(a) ") } println() }
showIntergerArray([1,2,3,4,5])
Swift Language-Function learning Part1