函数
函数的声明方式:
func 函数名(参数列表) -> 返回值 {
参数列表里的数据表示方式:
参数名:参数类型
函数体
}
案例:
struct Student {
var name = "name"
var index = 202100
var height = 160.00
var profession = "profession"
var sex = "男"
}
override func viewDidLoad() {
super.viewDidLoad()
var xiaoMing = Student()
xiaoMing.name = "xiaoMing"
xiaoMing.index = 202101
xiaoMing.height = 172.5
xiaoMing.profession = "计算机科学与技术"
xiaoMing.sex = "男"
xiaoMing = changeIndex(student: xiaoMing, index: 202110)
print("\(xiaoMing)")
}
func changeIndex(student:Student,index:Int) -> Student{
var newStudent = Student()
newStudent.index = index;
return newStudent
}
运行结果:
Student(name: "name", index: 202110, height: 160.0, profession: "profession", sex: "男")
可变参数函数
在定义函数时,如果不确定参数的数量,可通过在变量类型后面加(…)定义可变参数。一个函数最多能有一个可变参数,且必须是参数列表中的最后一个,这是为了避免产生歧义。
struct Student {
var name = "name"
var index = 202100
var height = 160.00
var profession = "profession"
var sex = "男"
var average = 0.0
}
override func viewDidLoad() {
super.viewDidLoad()
var xiaoMing = Student()
xiaoMing.name = "xiaoMing"
xiaoMing.index = 202101
xiaoMing.height = 172.5
xiaoMing.profession = "计算机科学与技术"
xiaoMing.sex = "男"
xiaoMing.average = average(scores:95.0,84.5,91.0,77.0)
print("\(xiaoMing)")
}
func average(scores:Double...) -> Double {
var sum:Double = 0.0
for score in scores {
sum += score
}
return sum/Double(scores.count)
}
运行结果:
Student(name: "xiaoMing", index: 202101, height: 172.5, profession: "计算机科学与技术", sex: "男", average: 86.875)
元组返回值类型函数
利用返回元组数据类型来 实现返回多个返回值。
override func viewDidLoad() {
super.viewDidLoad()
var xiaoMing = Student()
xiaoMing.name = "xiaoMing"
xiaoMing.index = 202101
xiaoMing.height = 172.5
xiaoMing.profession = "计算机科学与技术"
xiaoMing.sex = "男"
xiaoMing.average = average(scores:95.0,84.5,91.0,77.0)
let xiaoMingLevelInfo = getStudentLevelByIndex(index: xiaoMing.index)
print("\(xiaoMingLevelInfo)")
print("name:\(xiaoMingLevelInfo.name)")
}
func getStudentLevelByIndex(index:Int) -> (name:String,level:Int) {
if index==202101 {
let name = "xiaoMing"
let level = 9
return (name,level)
}
return ("",0)
}
运行结果:
(name: "xiaoMing", level: 9)
xiaoMing
传参到内部 修改外部的值
要在函数方法内改变外部的参数的值,就要在方法声明参数列表的时候,加上inout关键字,并且在传参的时候要加上符号&,表示传入参数是可以被改变的。
struct Student {
var name = "name"
var index = 202100
var height = 160.00
var profession = "profession"
var sex = "男"
var average = 0.0
var level = 0
}
override func viewDidLoad() {
super.viewDidLoad()
var xiaoMing = Student()
xiaoMing.name = "xiaoMing"
xiaoMing.index = 202101
xiaoMing.height = 172.5
xiaoMing.profession = "计算机科学与技术"
xiaoMing.sex = "男"
xiaoMing.average = average(scores:95.0,84.5,91.0,77.0)
setStudentLevel(student: &xiaoMing, level:9)
print("\(xiaoMing)")
}
func setStudentLevel(student:inout Student, level:Int){
student.level = level
}
运行结果:
Student(name: "xiaoMing", index: 202101, height: 172.5, profession: "计算机科学与技术", sex: "男", average: 86.875, level: 9)
常用的内置函数
- abs() 绝对值函数
- min() 最小值函数
- max() 最大值函数
- filter() 筛选条件函数 数组类型的成员函数,通常用于查找数组中满足条件的元素。
filter(includeElement: (T) -> Bool) -> T[] 传入filter()函数中的参数就是一个闭包。 - map() 通常用于将数组中的每个元素通过指定的方法进行转换。
- reduce() 传入两个参数,第一个参数是起始序列号,第二个参数是想要执行的运算。如:reduce(0,+),则表示的是从第0个元素开始,和后面的元素依次相加。
内嵌函数
在函数里面定义函数,常见于返回值为一个函数的情况。
|