函数用来完成特定任务的独立代码块
无参数,无返回函数
class tianxia{
func addOne(x:Int,y:Int) -> Int {
return x+y
}
}
let tian=tianxia.init()
tian .addOne(x: 5, y: 6)
//无参数函数
func sayHelloWorld(){
print("hello ,world")
}
sayHelloWorld()
//Constant 'result' inferred to have type '()', which may be unexpected ,这里没有返回值
let result=sayHelloWorld()
print(result) //所以这里输出()
func sayHelloToPerson(person:String) ->Void{
print("hello,\(person)!")
}
sayHelloToPerson(person: "tianxia") //有参数但是没有返回值
多重返回值函数
/** name:多重返回值函数
* @param (evens:Int,odds:Int) 被称为元组Tuple
* @return evens,odds
*/
func count(list:Array<Int>) ->(evens:Int,odds:Int){
var evens=0,odds=0
for number in list{
if(number%2==0){
evens+=1;
}else{
odds+=1;
}
}
return (evens,odds)
}
//用法
let total = count(list: [5,6,7,8,9])
print("\(total.evens) evens and \(total.odds) odds")
//将复合值赋给单个的值
let (evens,odds) = total
print(odds)
//定义元组Tuples
/**
* 把多个值组合成一个复合值
* 元组内的值可以是任意类型
* 可通过名字或者下标访问
* 最为返回值,非常有用
* 不适合创建复杂的数据机构
* 适合组织临时值
*/
//方式1
let http200Status = (200,"OK")
print(http200Status.0)
print(http200Status.1)
//方式2
let http200Status1=(statusCode:200,description:"OK")
print(http200Status1.statusCode)
print(http200Status1.description)
//将多个值组成一个复合值
let testTuple = (200,"OK",true,8.9,String(),Array(repeating:7, count:5))
参数
//参数
//正常的参数
func addThreeInts(x:Int,y:Int,z:Int) -> Int{
return x+y+z
}
addThreeInts(x: 1, y: 2, z: 3)
//当定义了外部参数时候,调用的时候需要使用外部参数
func addThreeInts(wbcs x:Int,wbss2 y:Int,z:Int) -> Int{
return x+y+z
}
addThreeInts(wbcs: 22, wbss2: 11, z: 1)
//如果调用参数想第一个参数不输入描述的话,需要在参数前加上 _
func addThreeInts(_ x:Int,wbss2 y:Int,z:Int) -> Int{
return x+y+z
}
addThreeInts(22, wbss2: 11, z: 11)
参数默认值
//参数默认值->适用范围给客户提供了api之后,自己这边又想要添加新的参数,而又不想客户那边调用参数进行变动,就用这种参数默认值,老的代码照样调用,新的代码还可以正常工作
//调用参数时候有默认值的如果是自己需要的可以不再传递参数
//用法 joiner:String="," 直接进行赋值,有参数默认值得话,要放到最后面,有多个的话就放在最后多个
func join(string s1:String,toString s2:String,withJoiner joiner:String=",") ->String{
return s1+joiner+s2
}
//这里只需要输入两个参数就可以了
join(string: "hello", toString: "every one")
常量参数与变量参数
/**
* 常量参数与变量参数
*/
//无法达到交换目的代码
func swapTwoInts( a:Int, b:Int){
let temporaryA=a
a=b //这里参数默认是常量,如果想要更改需要变成变量 Cannot assign to value: 'a' is a 'let' constant
b=temporaryA
}
//设置成inout变量的作用域会扩大到函数外,否则不会发生函数交换
func swapTwoInts1( a:inout Int, b:inout Int){
let temporaryA=a
a=b
b=temporaryA
}
var first=8
var second = 10
swapTwoInts1(a: &first, b: &second)
print(first) //10
可变参数
/**
* 可变参数Variadic Parameters
*/
//可变参数要放在最后,防止出现歧义
func average(numbers: Double...) ->Double{
var total:Double = 0
//这里可以将可变参数当做一个数组来取值
for number in numbers{
total+=number
}
return total/Double(numbers.count)
}
average(numbers: 1,4,5,6,7,8)
函数类型
/**
* name:函数类型
* connect:每个函数都有特定的函数类型,由函数的参数类型和返回类型组成,函数类型只跟参数类型和返回类型有关
*/
/**
* 可以定义常量和变量:将函数类型赋值给常量变量
*/
func addTwoInts(a:Int,b:Int)->Int{
return a+b
}
func multiplyTwoInts(a:Int,b:Int)->Int{
return a*b
}
//函数类型赋值给变量
var mathFunction : (Int,Int) ->Int = addTwoInts(a:b:)
mathFunction(3,5)//8
mathFunction=multiplyTwoInts(a:b:)
mathFunction(3,5)//15
//函数类型赋值给常量
let mathFunction2 : (Int,Int) -> Int = addTwoInts(a:b:)
mathFunction2(3,5)//8
//函数类型赋值给常量,简易写法
let mathFunction3=addTwoInts(a:b:)
mathFunction3(3,5)//8
/**
* 将函数类型作为参数
*/
func calculate(mathFunction:(Int,Int)->Int,a:Int,b:Int)->Int{
return mathFunction(a,b)
}
//这里将addTwoInts函数作为参数传进去,进行了addTwoInts函数的运算
calculate(mathFunction: addTwoInts(a:b:), a: 3, b: 5)//8
//同上,做了乘法运算
calculate(mathFunction: multiplyTwoInts(a:b:), a: 3, b: 5)//15
/**
* 函数类型作返回值
*/
//符合这种函数类型的参数 ((Int,Int)->Int) 进行返回addTwoInts(a:b:),multiplyTwoInts(a:b:)都是符合这个类型的
func chooseFunction(isAdd:Bool)->((Int,Int)->Int){
return isAdd ? addTwoInts(a:b:):multiplyTwoInts(a:b:)
}
//chooseFunction返回了一个函数给addFunction所以addFunction可以当函数使用
let addFunction = chooseFunction(isAdd: true)
addFunction(3,5)
//更简洁的使用方式
chooseFunction(isAdd: true)(3,5)//8
chooseFunction(isAdd: false)(3,5)//15
//可以将这个返回值是函数的,给那个接收函数为参数的函数
let addFunctionMain=chooseFunction(isAdd: true)
calculate(mathFunction: addFunction, a: 3, b: 5)//8
嵌套函数
/**
* 嵌套函数
*/
func quadraticsSumOfTwoInts(a:Int,b:Int)->Int{
//嵌套函数,外部无法访问
func addTwoInts(a:Int,b:Int)->Int{
return a+b
}
func squareOfInts(a:Int)->Int{
return a*a
}
return addTwoInts(a:squareOfInts(a: a),b: squareOfInts(a: b))
}
//要公开这写函数需要更改,将函数作为w返回值反回去
func chooseFunction(isAdd:Bool)->((Int,Int)->Int)
{
//嵌套函数,外部无法访问
func addTwoInts(a:Int,b:Int)->Int{
return a+b
}
func squareOfInts(a:Int,b:Int)->Int{
return a*b
}
return isAdd ? addTwoInts(a:b:):squareOfInts(a:b:)
}
quadraticsSumOfTwoInts(a: 5, b: 6)//61
let addFunciton=chooseFunction(isAdd: false)//返回一个函数
addFunciton(1,1)
网友评论