一、函数类型

Every function in Swift has a type, consisting of the function’s parameter types and return type.

Function Types

Every function has a specific function type, made up of the parameter types and the return type of the function.

For example:

  1. func addTwoInts(_ a: Int, _ b: Int) -> Int {
  2. return a + b
  3. }
  4. func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
  5. return a * b
  6. }

This example defines two simple mathematical functions called addTwoInts and multiplyTwoInts. These functions each take two Int values, and return an Int value, which is the result of performing an appropriate mathematical operation.

The type of both of these functions is (Int, Int) -> Int. This can be read as:

“A function that has two parameters, both of type Int, and that returns a value of type Int.”

Using Function Types

You use function types just like any other types in Swift. For example, you can define a constant or variable to be of a function type and assign an appropriate function to that variable:

  1. var mathFunction: (Int, Int) -> Int = addTwoInts

This can be read as:

“Define a variable called mathFunction, which has a type of ‘a function that takes two Int values, and returns an Int value.’ Set this new variable to refer to the function called addTwoInts.”

Function Types as Parameter Types

Here’s an example to print the results of the math functions from above:

  1. func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
  2. print("Result: \(mathFunction(a, b))")
  3. }
  4. printMathResult(addTwoInts, 3, 5)
  5. // Prints "Result: 8"

Function Types as Return Types

右侧结合律

Function Argument Labels and Parameter Names

Each function parameter has both an argument label and a parameter name. The argument label is used when calling the function; each argument is written in the function call with its argument label before it. The parameter name is used in the implementation of the function. By default, parameters use their parameter name as their argument label.

Specifying Argument Labels

  1. func greet(person: String, from hometown: String) -> String {
  2. return "Hello \(person)! Glad you could visit from \(hometown)."
  3. }
  4. print(greet(person: "Bill", from: "Cupertino"))

关键字:

In-Out Parameters

Function parameters are constants by default. Trying to change the value of a function parameter from within the body of that function results in a compile-time error.

Optional Tuple Return Types

An optional tuple type such as (Int, Int)? is different from a tuple that contains optional types such as (Int?, Int?). With an optional tuple type, the entire tuple is optional, not just each individual value within the tuple.

func minMax(array: [Int]) -> (min: Int, max: Int)?

返回值需要判断

swift语言点评五-Function的更多相关文章

  1. Swift语言指南(五)--数字字面量和数字类型转换

    原文:Swift语言指南(五)--数字字面量和数字类型转换 数字字面量 整数字面量写法如下: · 十进制数,无前缀 · 二进制数,以 0b 为前缀 · 八进制数,以 0o 为前缀 · 十六进制数,以 ...

  2. swift语言点评十五-$0

    import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...

  3. swift语言点评四-Closure

    总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...

  4. swift语言点评一

    一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...

  5. swift语言点评十九-类型转化与检查

    1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...

  6. swift语言点评十八-异常与错误

    1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...

  7. swift语言点评二

    一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...

  8. swift语言点评十七-Designated Initializers and Convenience Initializers

    Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...

  9. swift语言点评十六-Initialization && Deinitialization

    initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...

随机推荐

  1. SQL Server-简单查询语句,疑惑篇

      前言 对于一些原理性文章园中已有大量的文章尤其是关于索引这一块,我也是花费大量时间去学习,对于了解索引原理对于后续理解查询计划和性能调优有很大的帮助,而我们只是一些内容进行概括和总结,这一节我们开 ...

  2. mvc模式开发

  3. Pyhton学习——Day36

    #异步IO——Asynchronous#异步效率最高,特点:全程无阻塞# 在说明synchronous IO和asynchronous IO的区别之前,需要先给出两者的定义.# Stevens给出的定 ...

  4. Facebook再次爆出安全漏洞,9000万用户受影响

    今年上半年开始,美国社交媒体Facebook因数据泄露事件和涉嫌操纵选举等问题频繁接受听证会拷问,然而事情却远没有结束.今年9月Facebook再次爆出安全漏洞,导致9000万用户可能受到影响. 根据 ...

  5. 汇编(assembling)简介(源:阮一峰)

    简介 计算机真正能够理解的是低级语言,它专门用来控制硬件.汇编语言就是低级语言,直接描述/控制 CPU 的运行.如果你想了解 CPU 到底干了些什么,以及代码的运行步骤,就一定要学习汇编语言. 我们知 ...

  6. 作用域与this

    面向对象 一.单例模式 1.1 对象数据类型的作用: 把描述一个对象的属性和方法放在一个单独的空间,与其他的对象分割开,即时出现属性名相同的情况,也不会产生冲突 var name="xiao ...

  7. Github添加SSHkey

    Git详细教程可参考廖雪峰的Git教程 1. 打开 Git Bash,输入cd ~/.ssh——回车(看你是否有了ssh key 密钥,有了就备份): 2. 输入ssh-keygen -t rsa - ...

  8. jQuery Validate验证(项目中使用的)

    大致结构是: <script type="text/javascript" src="<%=path %>/js/jquery-1.9.1.min.js ...

  9. POJ 2661Factstone Benchmark(斯特林公式)

    链接:传送门 题意:一个人自命不凡,他从1960年开始每10年更新一次计算机的最长储存长数.1960年为4位,每10年翻一倍.给出一个年份y,问这一年计算机可以执行的n!而不溢出的最大n值 思路:如果 ...

  10. QT中文字的绘制

    为什么要做这次文字的介绍,因为在一般的教材中,还真没有文字的描述: 1.绘制最简单的文字. 我们更改重绘函数如下: void Dialog::paintEvent(QPaintEvent *){QPa ...