swift语言点评五-Function
一、函数类型
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:
func addTwoInts(_ a: Int, _ b: Int) -> Int {
return a + b
}
func multiplyTwoInts(_ a: Int, _ b: Int) -> Int {
return a * b
}
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:
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:
func printMathResult(_ mathFunction: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFunction(a, b))")
}
printMathResult(addTwoInts, 3, 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
func greet(person: String, from hometown: String) -> String {
return "Hello \(person)! Glad you could visit from \(hometown)."
}
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的更多相关文章
- Swift语言指南(五)--数字字面量和数字类型转换
原文:Swift语言指南(五)--数字字面量和数字类型转换 数字字面量 整数字面量写法如下: · 十进制数,无前缀 · 二进制数,以 0b 为前缀 · 八进制数,以 0o 为前缀 · 十六进制数,以 ...
- swift语言点评十五-$0
import UIKitimport XCPlayground class ViewController: UIViewController { func action() { print(" ...
- swift语言点评四-Closure
总结:整个Closure的作用在于简化语言表述形式. 一.闭包的简化 Closure expression syntax has the following general form: { () -& ...
- swift语言点评一
一.变量定义 1.常量与变量 Use let to make a constant and var to make a variable. 2.类型与推测 However, you don’t alw ...
- swift语言点评十九-类型转化与检查
1.oc比较: -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个 ...
- swift语言点评十八-异常与错误
1.错误类型与枚举的结合 enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNee ...
- swift语言点评二
一.数据类型 1.基础类型的封装 Swift provides its own versions of all fundamental C and Objective-C types, includi ...
- swift语言点评十七-Designated Initializers and Convenience Initializers
Swift defines two kinds of initializers for class types to help ensure all stored properties receive ...
- swift语言点评十六-Initialization && Deinitialization
initial value:必须初始化.不影响观察者 Classes and structures must set all of their stored properties to an appr ...
随机推荐
- navigate系列api
wx.navigateTo 用于保留当前页面.跳转到应用内的某个页面,使用 wx.navigateBack可以返回到原页面.对于页面不是特别多的小程序,通常推荐使用 wx.navigateTo进行跳 ...
- loging模块
logging模块 什么是logging模块 logging模块是python提供的用于记录日志的模块 为什么需要logging 我们完全可以自己打开文件然后,日志写进去,但是这些操作重复且没有任何技 ...
- debian 9 配置ati驱动
可以参考debian wiki 1.识别自己显卡驱动 lspci -nn | grep VGA 2.添加源 # Debian "stretch" deb http://httpre ...
- 新手学python-Day2-变量和循环判断
第二天作业: 初探三级菜单,凭现有知识,注意变量可以不声明,但要提前赋值! 此处shuru = '' 可以不写,因为第7行被赋值了,如果只调用shuru不赋值就会报错 shuru = '' sheng ...
- 小学生都能学会的python(列表[ ])
小学生都能学会的python(列表[ ]) 1. 什么是列表(list) 能装东西的东西 列表中装的数据是没有限制的, 大小基本上是够用的 列表使用[]来表示. 在列表中每个元素与元素之间用逗号隔开 ...
- STM32 软件模拟 IIC 代码,标准库、HAL库可用
#ifndef _IIC_H #define _IIC_H #include "stdio.h" #include "stm32f1xx_hal.h" /* 定 ...
- thinkPHP的Excel插件
原文地址 http://www.thinkphp.cn/topic/14005.html 总结的注意事项 1实例化第三方类,要在类名前加\ ,不然引用地址不对. 实现步骤:一:在http://phpe ...
- 【HDU 6299】Balanced Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们贪心地把每一个括号序列能匹配都按照栈的规则都匹配出来. (直接递增匹配对数*2就可以了 最后栈里面就只剩下类似))))((((( ...
- 洛谷——P1886 滑动窗口|| POJ——T2823 Sliding Window
https://www.luogu.org/problem/show?pid=1886#sub || http://poj.org/problem?id=2823 题目描述 现在有一堆数字共N个数字( ...
- 面试书上一些题目的整理:O(n)复杂度排序年龄 & 青蛙跳台阶
可以按照年龄的个数,设置99个桶,然后桶内处理. 青蛙跳台阶,每次1阶或者2阶,就是fib数 如果每次1到n阶,那么归纳法可得,是2^(n-1) 另外1*2 覆盖 2*n个矩阵的问题,仍然是Fib数. ...