Swift Language-Function learning Part1

Source: Internet
Author: User

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

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.