Swift function Chapter

Source: Internet
Author: User

A function is a self-contained block of code that performs a specific task. Given a function name identifier, this identity can be used for "invoke" when performing its task.

Swift's unified functional syntax is flexible enough to express anything, whether it is a simple C-style function expression without a parameter name, or a Objective-c language-style function that requires a complex name for each local parameter and external parameter. Parameters provide default values to simplify function calls and modify the passed variables by setting the input and output parameters when the function executes.

Each function in Swift has a type, including the parameter type and return type of the function. You can conveniently use this type like any other type, which makes it easy to pass functions as arguments to other functions, even returning function types from functions. Functions can also be written in other functions to encapsulate a function that is useful within the scope of a nested function.

Declaration and invocation of a function

When you define a function, you can define one or more names for it, define the type value as the input to the function (called the parameter), and return the type of the output definition (known as its return type) when the function finishes.

Each function has a function name that describes the task performed by the function. To use the function of a function, you make "call" by using its name, and match the parameter type of the function by its input value (called the parameter). The supplied arguments for a function must always be in the same order as the function argument list.

Example:

//function with no parameters, no return valuefunc HelloWorld () {println ("HelloWorld")}helloworld ()//functions with parameters that do not return a valueFunc Printstr (str:nsstring!) {println (STR)}printstr ("123456")//functions with parameters and return valuesFunc Add (One:int,two:int),int{returnOne +Two}println ("1+2 = \ (add )")//variable ParametersFunc addallnumbers (Numbers:int ...) -int{varSum:int =0     forIinchNumbers {sum+=i}returnsum}//parameter has a default valueFunc statentmentstr (name:nsstring!,fstr:nsstring ="My name is:") {println (Fstr+name)} Statentmentstr ("Xiami")//See the Post asking if the reference type will change as a parameterclassa{varValue:int =1}varAA =A () func Changeclass (item:a) {Item.value=Ten}changeclass (aa) AA//This shows that the parameters of the reference type as a function can vary.//input and Output typefunc swaptwovalue (inout firstvalue one:int,inout two:int) {Let value= One One= Both=Value}varOne:int =TenvarTwo:int = -Swaptwovalue (firstvalue:&one, &Both ) Onetwo//In the example above, Firstvalue is the name of the external parameter, which is equivalent to the description, which can be used to know what this parameter is when externally invoked .

Swift function Chapter

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.