函數(shù)的幾種類型
1.無參無返
func great() -> Void {
}
func great() {
}
2.有參無返
func includeParameterNoneReturnValue(person: String, day : String) {
print("Hello \(person), today is \(day)")
}
includeParameterNoneReturnValue(person: "Judy",day: "23")
2.1 省略外部參數(shù)名
func omitOutParameter(_ person : String, _ day : String) -> String {
return "Hello \(person), today is \(day)."
}
print(omitOutParameter("流川楓", "8"))
3.有參有返
func includeParameterAndReturnValue (_ person : String, _ day : String) -> String {
return "Houston player \(person) join at \(day)"
}
print(includeParameterAndReturnValue("周琦", "9"))
4.無參有返
func NoneParameterReturnValue () -> String {
return "I hate bitch!"
}
參數(shù)和返回值
1.參數(shù)可以是那些兴垦?
基本類型的值,對象,數(shù)組,字典,元組,可變數(shù)量的參數(shù),函數(shù),閉包函數(shù),協(xié)議,結(jié)構(gòu)體,枚舉值
2.怎么定義參數(shù)
//a.單值
func calculate(a:Int) {
let b = a
}
//b. 多值
func calculates(a: Int...) {
for _ in a {
}
}
//c. 元祖
func calculatess(a:(name:String, age:Int)){
let name = a.name
let age = a.age
}
//d. 數(shù)組
func calculate_s(a:[String]){
for student in a {
}
}
//e. 字典
func calculate_d (a:[String:Int]){
for student in a {
print(student.key)
print(student.value)
}
}
//f. 函數(shù)作為參數(shù)
func add(a:Int,b:Int) -> Int {//作為函數(shù)參數(shù)的函數(shù)
return a + b
}
func calculate_fa(asd:(Int,Int) -> Int) {//定義的參數(shù)為函數(shù)的函數(shù)
print( asd(3,5))
}
calculate_fa(asd: add(a:b:))
//g. 閉包寫法
calculate_fa { (a, b) -> Int in
return a + b
}
calculate_fa{ (a, b) in a + b }//省略寫法(由于swift有推斷能力勉抓,這樣寫就能夠幫你推斷出上面的寫法)
//h. 參數(shù)為協(xié)議的方法
// protocol Player{ // 定義協(xié)議
// func play()
// }
//
// func playMusicWithPlayer(player:Player){
// player.play()
// }
//i. 參數(shù)為結(jié)構(gòu)體
// struct Student{
// var name:String
// var age:Int
// };
//
// func getStudentDescription(student:Student){
// print(student.name)
// print(student.age)
// }
//j. 參數(shù)為枚舉類型
// 定義枚舉值
enum CarType:String{
case Lincoln = "林肯"
case MERCURY = "水星"
case SUZUKI = "鈴木"
}
// 參數(shù)為協(xié)議的方法
func describeCar(carType:CarType){
print(carType.rawValue);
}
函數(shù)內(nèi)部定義函數(shù)
func getValueByFlag(flag:Bool) -> (Int, Int) -> Int {
func add_f(a:Int, b:Int) -> Int {
return a + b
}
func descrase(a:Int,b:Int) -> Int {
return a - b
}
if flag {
return add
} else {
return descrase
}
}
let addFunc = getValueByFlag(flag: false)
print(addFunc(2,4))
設(shè)置默認參數(shù)值
func addStudent(student:(name:String, nunbers:Double)=("姓名",23554411)){
print(student.name)
print(student.nunbers)
}
addStudent()
addStudent(student: ("元昊",111307302))
inout的使用
需求: 創(chuàng)建一個函數(shù),交換兩個Int類型值
a.如果參數(shù)為let修飾的常量
func swapTwoInts( a: Int, b: Int){
let temporaryA = a
a = b
b = temporaryA
}
報錯:系統(tǒng)提示錯誤,說常量不能修改值
b.我們將參數(shù)變成變量var
func swapTwoInts( var a: Int, var b: Int){
let temporaryA = a
a = b
b = temporaryA
}
報錯,不能使用var 修飾參數(shù)
c.inout 修飾的參數(shù)可以修改值
func swapTwoInts( a: inout Int, b:inout Int){
let temporaryA = a
a = b
b = temporaryA
}
var a = 30
var b = 40
swapTwoInts(a: &a, b: &b)
print(a)
print(b)
40
30
Warning:
1.inout的位置 在: 后面,數(shù)據(jù)類型前面
2.inout 修飾的參數(shù)不能有默認值
3.inout 不能用于修飾多值(如a:Int...)
others:函數(shù)類型的變量不能用標簽修飾參數(shù)
// 錯誤的寫法 不能使用a,b標簽
var swap1:( a :inout Int, b: inout Int)->Void = swapTwoInts
// 你應該像下面這樣
var swap1:( _ :inout Int, _: inout Int)->Void = swapTwoInts
// 或者下面這樣也可以,a,b 不一定要和實際函數(shù)對應
var swap1:( _ a:inout Int, _ b: inout Int)->Void = swapTwoInts
// 建議還是用下面這種
var swap1:( inout Int, inout Int)->Void = swapTwoInts
定義閉包類型數(shù)據(jù)
var customersInLine = ["Chirs","Alex","Ewa"]
let customerprovider = { customersInLine.remove(at: 0)}
print(customersInLine.count)
print("Now Serving \(customerprovider())!")
print(customersInLine.count)
提示:上面那種閉包其實是無參有返的閉包形式,原形如下
let customerProvider:()->String= { customersInLine.remove(at: 0)}