Function
A function is a self-contained block of code that performs a specific task. Identify what it is by giving a function name and use that name to invoke the function when needed to perform the task.
Swift's unified functional syntax is flexible enough to express anything, whether it is a simple style function with no parameter name, or a complex objective-c style method with local and external parameter names. Parameters can provide default values for simple function calls and can be passed as input/output parameters, modifying the passed variables as the function executes.
Each function in Swift has a type, including the parameter type and return type of the function. You can use this type as you would with other types in swift, which makes it easy to pass functions as arguments to other functions, even returning function types from functions. Functions can also be written to other functions in order to encapsulate useful functionality in function-in-action.
Defining and calling Functions
When you define a function, you can optionally define one or more names, the type value as the input to the function (called the formal parameter), or the type of the return value (called the return type) after the end of a function. Each function has a function name that describes the task performed by the function. To use a function, you can use its name for "call" and match the function's parameter type through its input value (called the argument--argument). The argument (arguments) of a function must always be in the same order as the function parameter (parameter).
For example, the function called in the following example is Greetingforperson, as it describes-it requires a person's name as input and returns a greeting for that person. To implement this function, you define an output parameter-a string value named PersonName, and a string return type that contains a greeting for that person:
Func SayHello (personname:string), String { "" "! " return
Closed-Packet expression Nested functions are a convenient way to name and define self-contained code modules in more complex functions. Of course, it is sometimes useful to write small, non-fully defined and named class function structures, especially when dealing with functions and requiring additional functions as arguments to the function. A closure expression is a way to construct an inline closure using concise syntax. The closure expression provides some syntax optimizations that make writing closures straightforward. The following example of a closure expression shows how the sort function is defined and how the syntax is optimized by using several iterations. Each iteration describes the same functionality in a more concise manner. The closure expression syntax has the following general form:
Inch
The closure expression syntax can use constants, variables, and inout types as parameters, but does not provide a default value. You can also use variable parameters at the end of the parameter list. Tuples can also be used as parameters and return values. The following example shows the code for the closure expression version of the previous backwards function: It is important to note that the inline closure parameter and the return value type declaration are the same as the backwards function type declaration. In both of these ways, the type of bool (s1:string, s2:string) is written. In an inline closure expression, however, the function and the return value
inch return s1 >
Types are written in curly braces, not outside curly braces. The function body part of the closure is introduced by the keyword in. The keyword indicates that the closure parameter and the return value type definition have been completed and the closure function body is about to begin. Because the function body part of the closure is so short that it can be rewritten into one line of code:
inch return
Swift Learning the next day