/** * 函数是Swift的重要组成单位,一个Swift程序可以包含很多函数 */
//函数定义和调用
//定义一个函数,声明2个形参,返回Int类型
func max(x:Int,y:Int) -> Int
{
//定义一个变量z,等于xy中较大的值
var z = x > y ? x : y
return z
}
//声明一个函数,声明一个形参,返回String类型
func sayHi (name:String) -> String
{
println("正在执行sayHi()函数")
return "\(name),你好!"
}
var a = 6
var b = 9
var result = max(a,b)
println(result)//9
println(sayHi("老师"))//"老师,你好!"
//函数返回值
//1.无返回值,无参
func name()
{
println("执行name函数")
}
//2.没有返回值,有1个形参
func sayName(n:String) ->()
{
println("\(n),好呀!")
}
//3.没有返回值有两个形参
func showPeople(msg:String,name:String,age:Int) ->Void
{
for _ in 1...age
{
println(msg)
}
}
name()
sayName("huang")
println(showPeople("Swift","c", 4))
//4.函数返回值支持元组类型
//返回值是(String,String)
func diva(num:Double) ->(String,String)
{
var zheng = Int64(num)
var xiao = round((num - Double(zheng)) * 100)
return ("\(zheng)","\(Int(xiao))")
}
var result1 = diva(1234.3421)
println("\(result1.0)")
println("\(result1.1)")
//5.为返回值元组指定名称
func getMaxAndMin(nums:[Int]) ->(max:Int,min:Int)
{
var max = nums[0]
var min = nums[0]
for n in nums
{
if n > max {
max = n
}
if n < min {
min = n
}
}
return(max,min)
}
var numss = [20,453,342,35,76,324,65,14,]
var maxAndmin = getMaxAndMin(numss)//(.0 453,.1 14)
//递归函数,函数调用它自身
func fu(n:Int) -> Int
{
if n == 0 {
return 1
}else if n == 1 {
return 4
}else {
//函数中调用它自身,就是函数递归
return 2 * fu(n - 1) + fu(n - 2)
}
}
println("\(fu(10))")//10497
//函数的形参
//1.外部形参名及其简化形式
func girth(width: Double,height: Double) ->Double
{
return Double(2) * (width + height)
}
println(girth(12,23.7))
//2.指定外部形参名,外部形参名 局部变量名 : 形参类型
func girth1(高 height: Double,宽 width: Double) ->Double
{
return Double(2) * (height + width)
}
println(girth1(高: 12.4,宽: 33))//90.8
//3.指定外部形参名,当外部形参名和局部变量名一样时,可以在局部变量名前加#,外部形参名 局部变量名 : 形参类型
func girth2(#height: Double,#width: Double) ->Double
{
return Double(2) * (height + width)
}
println(girth2(height: 23,width: 34.3))//114.6
//4.可以省略其中一个局部变量的外部形参名
func girth3(#height: Double,width: Double) ->Double
{
return Double(2) * (height + width)
}
println(girth3(height: 12.4, 45))//114.8
//5.设置形参默认值
func people(姓名 name: String = "杰瑞",age: Int)
{
println("\(name),\(age)")
}
println("\(people(12))")
people(姓名:"jumjum", 15)
//6.设置默认形参,默认值应放在后面
func people1( age: Int,姓名 name: String = "杰瑞")
{
println("\(name),\(age)")
}
println("\(people1(12))")
people1(15,姓名:"jumjum")
//7.取消带默认值的外部形参名,用_空格代替外部形参名
func people2(age: Int,_ name: String = "杰瑞")
{
println("\(age),\(name)")
}
println("\(people2(14))")
//8.个数可变的形参(...),一个函数最多只能包含一个个数可变形参,个数可变形参的本质是一个数组类型的形参
func text(a : Int,names : String...)
{
for a in names {
println(a)
}
println(a)
}
text(12,"name1","name2")
//9.常量形参和变量形参
//在一些情况下程序希望直接使用函数形参作为变量,形参默认的是常量let声明,不能在函数体内对形参常量赋值
//使用var定义变量形参
func girth(var #width : Double,#height : Double) -> Double
{
//width,height 是变量形参,因此可以重新赋值
width = (width + height) * 2
return width
}
girth(width: 12.4,height: 34)//92.8
//10.In-Out形参,需要把值类型的参数传入函数内部
//交换两个参数的值
func swap(inout a : Int,inout b : Int)
{
let num = a
a = b
b = num
}
var a = 6
var b : Int = 9
swap(&a,&b)
println("a is \(a),b is \(b)")//"a is 9,b is 6"
//引用类型的参数传递
class DataWrap
{
var a :Int = 0
var b :Int = 0
}
func swa(var dw : DataWrap!)
{
//交换dw的a,b变量的值
var num = dw.a
dw.a = dw.b
dw.b = num
println("dw.a = \(dw.a),dw.b = \(dw.b)")
}
var dw = DataWrap()
dw.a = 6
dw.b = 9
swa(dw)//"dw.a = 9,dw.b = 6"
func swa1(inout dw : DataWrap!)
{
//交换a.b的变量
var tmp = dw.a
dw.a = dw.b
dw.b = tmp
println("a is \(dw.a),b is \(dw.b)")
//如果存在dw = nil 运行swa1后,不能访问dw
dw = nil
}
var dw1 : DataWrap! = DataWrap()
dw1.a = 5
dw1.b = 9
swa1(&dw1)
//println("\(dw1.a)")//报错
部分内容来自《疯狂Swift讲义》