今天我们要讲的就是函数【对于函数,在最后面还有几道题,喜欢的博友可以看了自己做一下,和我交流一下】

当然这与我们的c语言还是有一定的共同之处的,对于有一些c语言或者是java基础的童鞋,我觉得是很容易的。

定义函数的语法为:【注意一些书写格式】

    func 函数名(参数1: 类型1,参数2:类型2)-> 返回类型{代码块}

在本节课我们将涉猎到函数的参数为函数返回值为函数等情况

第二课时:

  1 //: Playground - noun: a place where people can play
  2 
  3 import UIKit
  4 //基本的函数
  5 func first() {
  6     print("first")
  7 }
  8 first()
  9 
 10 func first2() -> String {
 11     return "abc"
 12 }
 13 first2()
 14 let r = first2()
 15 r
 16 let _ = first2() //下划线忽略不管
 17 
 18 func first3(a:Int) ->Int {
 19     print(a)
 20     return 5
 21 }
 22 first3(a: 4)
 23 //first3(3) //不能省略a
 24 
 25 //subString(startIndex:1,length:3)
 26 //func add(firstNumber: Int ,addWithSecond: Int) -> Int {
 27 //    return firstNumber + addWithSecond
 28 //}
 29 //add(firstNumber: 3, addWithSecond: 4)
 30 //add(addWithSecond: 6, firstNumber: 5 )//不能改变顺序
 31 //参数忽略的问题
 32 //func add(_ firstNumber: Int ,addWithSecond: Int) -> Int {
 33 //    return firstNumber + addWithSecond
 34 //}
 35 //add(5, addWithSecond: 6)
 36 
 37 //func add(_ firstNumber: Int ,_ addWithSecond: Int) -> Int {
 38 //    return firstNumber + addWithSecond
 39 //}
 40 //add(50, 6)
 41 
 42 
 43 
 44 //a,和b成为外部参数,argument label(参数标签)
 45 //a和b不能在函数体内使用,只能调用时使用
 46 //func add(a firstNumber: Int ,b addWithSecond: Int) -> Int {
 47 //    return firstNumber + addWithSecond
 48 //}
 49 //add(a:5, b: 6)
 50 
 51 
 52 /************  可变长度,默认值 */
 53 
 54 //code snippet
 55 
 56 //参数可变长度
 57 //func add(numbers: Int...) -> Int {
 58 //    var total = 0
 59 //    for item in numbers {
 60 //        total += item
 61 //    }
 62 //    return total
 63 //}
 64 //add(numbers: 1,2,3)
 65 
 66 //参数有默认值
 67 
 68 //func add(num1: Int = 2,num2: Int = 3) ->Int{
 69 //    return num1 + num2
 70 //}
 71 
 72 //add(num1: 5)
 73 //add(num1: 2, num2: 5)
 74 //
 75 //add()
 76 //add(num1: 5, num2: 6)
 77 //add(num2: 3)
 78 //add(num2: 5, num1: 6) //不能改变顺序
 79 
 80 
 81 //func add(num1: Int = 2,num2: Int = 3,num3: Int) ->Int{
 82 //    return num1 + num2 + num3
 83 //}
 84 //add(num3: 5)
 85 //add(num1: 1, num2: 2, num3: 3)
 86 //add(num1: 4,  num3: 6)
 87 
 88 /******* 函数类型
 89  在swift中,函数是第一等的公民,意味着与类同级别
 90  */
 91 let f: (Int,Int)->Int
 92 func test1(a: Int,b: Int) -> Int {
 93     return 5
 94 }
 95 f = test1
 96 f(5, 6)
 97 
 98 let f2: ()-> Void
 99 func test2(){
100     
101 }
102 f2 = test2
103 f2()
104 
105 
106 
107 
108 
109 
110 func outer(inner:(Int)->Void) {
111     inner(5)
112 }
113 func test3(a: Int)  {
114     print(a)
115 }
116 outer(inner: test3)
117 
118 func demo(doFilter:(Int)-> Bool ) ->[Int]{
119     let arr = [1,2,6,8,99,6,33]
120     var result :[Int] = []
121     for item in arr {
122         if doFilter(item){
123             result.append(item)
124         }
125         
126     }
127     return result
128 }
129 
130 func guolv1(a: Int) -> Bool {
131     if  a > 30  {
132         return true
133     }
134     return false
135 }
136 
137 func guolv2(a: Int) -> Bool {
138     if  a % 2 == 0  {
139         return true
140     }
141     return false
142 }
143 
144 
145 let rr = demo(doFilter: guolv2)
146 rr
147 
148 func each(handler: (Int,Int)->Void) {
149     let arr = [1,2,6,8,99,6,33]
150     
151     for i in 0..<arr.count {
152         handler(i,arr[i])
153     }
154 }
155 
156 func myHandler(index: Int,value: Int)  {
157     print("index is \(index) value is \(value)")
158 }
159 
160 each(handler: myHandler)
161 
162 func aaa() -> Void {
163     print("this fun is called outer")
164 }
165 
166 func outer2() -> ()->Void {
167    
168     return aaa
169 }
170 
171 func outer3() -> ()->Void {
172     func bbb() -> Void {
173         print("this fun is called outer")
174     }
175     return bbb
176 }
177 let recFun = outer2()
178 recFun()
179 
180 outer3()()
181 
182 
183 func outer4(f:()->Void) -> (Int,Int)->(Int,Int) {
184     
185     f();
186     func temp(a: Int,b: Int)-> (Int,Int) {
187         
188         return(a + b,a * b)
189     }
190     return temp
191 }
192 func forOuter4() {
193     print("for outer4")
194 }
195 let outer4Result  = outer4 (f:forOuter4)
196 let outer4ResultResult = outer4Result(5,6)
197 outer4ResultResult.0
198 outer4ResultResult.1
199 
200 
201 
202 /*作业
203  1.写一个函数,有一个参数参数,表示的意思是记录的总条数
204  ,第二个参数,表示每页显示的记录数量,默认值为2
205  函数返回的结果就是一个Int的数组,表示页码
206  generatePages(records: Int,pageSize: Int = 2){
207  
208  }
209  generatePages(7)  ->   [1,2,3,4]
210  
211  2。找出swift语言的类似Math.Ceiling的函数出来
212  
213  3.设计一个函数,在其基础上,生成一系列的超链接字符串数组
214  ["a.aspx?pageno = 1",
215  "a.aspx?pageno = 2
216  "a.aspx?pageno = 3
217  ]
218  
219  
220  */

IOS系列之二就到这里结束了,其实函数的知识还有很多,就不在这里一一解剖了.