文章目录
  1. 1. Swift学习-06 函数基本用法
    1. 1.1. 简单函数
    2. 1.2. 使用元组让函数返回多个值
    3. 1.3. 内部参数名和外部参数名
    4. 1.4. 参数的默认值
    5. 1.5. 参数可变函数
    6. 1.6. inout,方法修改参数值
    7. 1.7. 函数类型
    8. 1.8. 将函数作为参数传递

Swift学习-06 函数基本用法

简单函数

1
2
3
4
5
6
7
8
9
10
11
//---------------6.1 swift函数的基本用法---------------//
func sayHello(name:String, greeting:String) ->String
{
let result = greeting + "," + name ?? "Guest" + "!"
return result
}
var nickName:String?
nickName = "jackChang"
println(sayHello(nickName!,"Good morning"))

使用元组让函数返回多个值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
func maxminScores (scores:[Int]) -> (maxScore:Int, minScore:Int)?{
if scores.isEmpty{
return nil
}
var curmax = scores[0], curmin = scores[0]
for score in scores[1..<scores.count]
{
curmax = max(curmax, score)
curmin = min(curmin, score)
}
return (curmax, curmin)
}
var userScoures:[Int]? = [12, 990, 572, 3258, 9999, 1204]
userScoures = userScoures ?? []
if let result = maxminScores(userScoures!)
{
println("The maxsocre user score is : \(result.maxScore)")
}

内部参数名和外部参数名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
func sayHello2(userName name:String, greetingWord greeting:String) ->String
{
let result = greeting + "," + name ?? "Guest" + "!"
return result
}
sayHello2(userName: "jackChang", greetingWord: "Hello")
func sayHello3( #name:String, #greeting:String) ->String
{
let result = greeting + "," + name ?? "Guest" + "!"
return result
}
sayHello3(name: "jackChang", greeting: "Hello")

参数的默认值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func sayHello4(name:String, greeting:String = "Hello") ->String
{
let result = greeting + "," + name ?? "Guest" + "!"
return result
}
sayHello4("jackChang")
sayHello4("jackChang", greeting:"Good Morning")
func sayHello5(name:String, greeting:String = "Hello" , others:String = "How are you") ->String
{
let result = greeting + "," + name ?? "Guest" + "!" + others
return result
}
sayHello5("jackChang")
sayHello5("jackChang", greeting:"Good Morning")
sayHello5("jackChang", others: "How do you do")
sayHello5("jackChang", others: "Hello", greeting: "How do you do")

参数可变函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func add(a:Int , b:Int, others:Int ...) -> Int
{
var result = a + b
for num in others{
result += num
}
return result
}
var res = add(1,2)
add(4, 5, 6)
//变量参数和inout参数
//var num = 6
func toBinary(var num:Int) -> String{
var result:String = ""
while num != 0
{
result = String(num%2) + result
//允许修改参数,在方法名里面添加var
num /= 2
}
return result
}
var num = 6
toBinary(num)
//方法内变量值改变,方法外不变
num

inout,方法修改参数值

1
2
3
4
5
6
7
8
9
10
11
12
13
func swapTwoInts ( inout a:Int, inout b:Int)
{
let t = a;
a = b;
b = t;
}
var x = 0, y = 100
x
y
swapTwoInts(&x, &y)
x
y

函数类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
func add2(a:Int, b:Int) -> Int
{
return a + b
}
let anotherAdd:(Int,Int) -> Int = add2
func changeScores( op:(Int)->Int, inout scores:[Int] ){
for i in 0..<scores.count
{
scores[i] = op(scores[i])
}
}
func op1(x:Int)->Int { return Int(sqrt(Double(x))*10) }
func op2(x:Int)->Int { return Int( Double(x) / 150.0*100.0 ) }
func op3(x:Int)->Int { return x + 3 }
var scores1 = [36,61,78,89,99]
changeScores(op1, &scores1)
scores1
var score2 = [88,101,124,137,150]
changeScores(op2, &score2)
score2
var score3 = [59,61,76,83,95]
changeScores(op3, &score3)
score3

将函数作为参数传递

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var arr = [Int]()
for _ in 1...20
{
arr.append( Int(arc4random()%100))
}
arr
sort(&arr)
//从大到小
func compareTwoInts1(a:Int, b:Int) -> Bool{return a>b}
sorted(arr, compareTwoInts1)
//距离50的远近
func compareTwoInts2(a:Int, b:Int) ->Bool{return fabs(Float(a-50)) < fabs(Float(b-50))}
sorted(arr, compareTwoInts2)
arr
//比较字符串
var strArr = ["d","cd","bcd","abcd","abc","ab","a"]
sorted(strArr)
func compareTwoString( s1:String , s2:String ) -> Bool{
return countElements(s1) < countElements(s2)
}
sorted(strArr,compareTwoString)
func compareStringDefault(str1:String,str2:String) -> Bool{
return str1>str2
}
sorted(strArr, compareStringDefault)
strArr




文章目录
  1. 1. Swift学习-06 函数基本用法
    1. 1.1. 简单函数
    2. 1.2. 使用元组让函数返回多个值
    3. 1.3. 内部参数名和外部参数名
    4. 1.4. 参数的默认值
    5. 1.5. 参数可变函数
    6. 1.6. inout,方法修改参数值
    7. 1.7. 函数类型
    8. 1.8. 将函数作为参数传递