swift-function Learning

Source: Internet
Author: User

Functions: Functions are separate blocks of code that perform specific tasks.

Swift's uniform function syntax is flexible enough to express anything with simple C-type functions without parameter names, local and external complex Objective-c-style method parameter names for each parameter. Once the function has completed its execution, the parameter can provide a default value to simplify the function call and can be passed as an input and output parameter, modifying a passing variable.

Each function in Swift has its own type, including the parameter type and return type of the function. This type is similar to any other type in Swift, and you can use this type to make it easy to pass functions as arguments to other functions and return functions from functions. Functions can also be written in other functions to encapsulate useful functionality within the scope of a nested function.

To define and invoke a function:

When you define a function, you can optionally define one or more named type values, functions as input (called parameters), and/or a value of type, when it is finished the function is returned as output (known as its return type).

Each function has a function name that describes the task that the function performs. Using a function, you "call" the function with its name and pass it to the input value (called the parameter) of the matching function parameter type. The arguments to the provided function must be the same as the function's argument list.

A simple example: Define a function called SayHello, a parameter of type string personname, and the return value is a string type:

Single parameter Func  SayHello (personnmae:string)->string{let greatname = "Hello  " + Personnmae + "!" Return Greatname}

Write an output statement:

println (SayHello ("Tom"))

You can see the output in the console at this point:"Hello tom!"

A simple analysis of this code block: The FUNC keyword is to declare a function, SayHello is a defined function name, PersonName is a parameter of type string, which is the return value of the function, followed by the return type of the function.

If you want the function to have two parameters, refer to the C language, you can easily think of the following way, enter the parameters of the two int type, calculate the and:

Func sum (one:int,two:int)->int{return one + two}println (sum (2,4))
The result of this output is 6.

Note: When you have multiple parameters in a function, you need to separate them with commas.


parameterless function: The function is not required to define the input parameters. This is a function that has no input parameters, and when called, it always returns the same string message.
Parameterless function func noprame ()->string{    return "This is an parameterless function"}println (Noprame ())

Although the function does not take any arguments, the function name still needs to be placed in parentheses. When a function is called, there is a pair of empty parentheses after the function name.

No return value function: The function is not required to define the return type. This defines a noreturn function, which prints its own string value instead of returning it:

There is no return value function func Noreturn (onewords:string) {println ("This is \ (onewords)")    }noreturn ("a word")//Output inside function: a sentence

Functions with multiple return values: The following example defines a named Count function, which calculates vowels, consonants, and other characters in a string, based on the standard setting of vowels and consonants used in American English.

Functions with multiple return values, tuple type as function return type Func count (string:string)--(Vowels:int,consonants:int) {var vowels = 0,consonants = 0 For    character in string{        switch string (character). lowercasestring{case        "A", "E", "I", "O", "U":            + + Vowels case            "b", "C", "D", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "s", "T", "V", "w", "X", "Y", "Z": 
   ++consonants        Default:            break        }    }    return (vowels,consonants)}

Here is the call to the Count function:

Let total = count ("Some arbitrary string!") println ("\ (total.vowels) vowels and \ (total.consonants) consonants")

The console outputs:"6 vowels and consonants"

swift-function Learning

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.