//Functions
//带参数的函数
func sayHello(personName: String) ->String{
let greeting = "Hello, " + personName + "!"
return greeting
}
sayHello("Cjoe")
//不带参数是的函数
func sayHello2() ->String{
return "hello world"
}
sayHello2()
//带多个参数的函数
func sayHello1 (personName:String, alreadyGreeted:Bool) ->String{
if alreadyGreeted{
return sayHello(personName)
}else{
return sayHello1("Tim", alreadyGreeted: true)
}
}
sayHello1("Cjoee", alreadyGreeted: false)
//有参数无返回值
func sayGoodbye(presonName: String){
print("Goodbye, \(presonName)!")
}
sayGoodbye("Cjoee")
func sayGoodbye1(presonName: String) ->(){
print("Goodbye, \(presonName)!")
}
sayGoodbye1("Cjoee")
func sayGoodbye2(presonName: String) ->Void{
print("Goodbye, \(presonName)!")
}
sayGoodbye2("Cjoee")
//有参数有一个返回
func printAndCount(stringToPrint: String) -> Int {
print(stringToPrint)
return stringToPrint.characters.count //2
}
func printWithoutCounting(stringToPrint: String) {
printAndCount(stringToPrint) //12
}
printAndCount("hello, world")
// prints "hello, world" and returns a value of 12
printWithoutCounting("hello, world")
// prints "hello, world" but does not return a value
//有参数有多个返回值
func minMax(array: [Int]) ->(min: Int, max:Int){
var currentMIn = array[0] //23
var currentMax = array[0] //23
for value in array[1..<array.count]{
if value < currentMIn{
currentMIn = value
}else if value > currentMax{
currentMax = value
}
}
return (currentMIn,currentMax)
}
minMax( [23, 33 , 12 , 45 , 12 , 2 ]) //(.0 2, .1 45) 返回的是一个元组
let bounds = minMax([23,3,23,2345,55])
print("min is \(bounds.min) and max is \(bounds.max)") //"min is 3 and max is 2345\n"
//参数为int
func someFunction(firstParameterName: Int, secondParameterName: Int) {
// function body goes here
// firstParameterName and secondParameterName refer to
// the argument values for the first and second parameters
}
//someFunction(firstParameterName: 1, 2) false 第一个参数的参数名必须省略 第二个不可以
someFunction(1, secondParameterName:2)
//参数为string
func sayHello(to person: String, and anotherPerson: String) -> String {
return "Hello \(person) and \(anotherPerson)!"
}
print(sayHello(to: "Bill", and: "Ted")) //两个标志不可省略 参数名可以省略
// Prints "Hello Bill and Ted!"
//如果想省略函数名 和标志 使用下划线underScore
func someFunction (firstParameterName:Int, _ secondParameterName:Int){
}
someFunction(1, 3)
//设置参数的默认值
func someFunction1(parameterWithDefault: Int = 12){
parameterWithDefault //6, 12
}
someFunction1(6)
someFunction1()
//numbers: Double...代表numbers的个数为参数的个数
func arithmeticMean(numbers: Double...) ->Double{
var total: Double = 0
for number in numbers{
total += number
}
return total / Double(numbers.count)
}
arithmeticMean(1,3,4,4,5)
//交换值 inout inout _
func swapTwoInts(inout a: Int, inout _ b: Int){
let temporaryA = a
a = b
b = temporaryA
}
var someInt = 3
var anotherInt = 14
swapTwoInts(&someInt, &anotherInt)
func addTwoInts(a: Int, _ b:Int) -> Int{
return a + b
}
addTwoInts(23, 1)
//_代表函数调用的时候省略函数的提示词 以及函数名称
func multiplyTwoInt(a:Int, _ b: Int) -> Int{
return a * b
}
multiplyTwoInt(2, 3)
func printHelloWorld(){
print("hello word")
}
printHelloWorld()
//---------------------------------------care for -----------------------------------------
//返回函数类型 mathFunction 的参数类型和addTwoInts 的一样 所以可以吧addTwoInts的方法赋给 mathFunction
var mathFunction:(Int,Int) ->Int = addTwoInts
mathFunction(3,6)//9
mathFunction = multiplyTwoInt
mathFunction(3,7) //21
let anotherFunction = addTwoInts
anotherFunction(3,8) //11
//函数类型作为参数类型
func printMathResult(mathFunction :(Int, Int) -> Int, _ a:Int, _ b: Int){
print("result: \(mathFunction(a ,b))") // 5
}
printMathResult(addTwoInts , 2, 3)
//函数类型作为范围类型
func stepFoword(input: Int) ->Int{
return input + 1
}
func stepBackForword(input:Int) -> Int{
return input - 1
}
func chooseStepFunction(backwards: Bool) ->(Int) ->Int{ //(Int) ->Int 代表的是一个函数类型 (backwards: Bool)函数参数
return backwards ? stepBackForword : stepFoword
}
var currentValue = 3
let moveNearerZero = chooseStepFunction(currentValue > 0)
while currentValue != 0 {
currentValue = moveNearerZero(currentValue)
}
//print("zero!")
//嵌套函数
func chooseStepFunction1(backwards :Bool) ->(Int) ->Int{
func stepForward(input: Int) ->Int{
return input + 1
}
func stepFoword(input: Int) ->Int{
return input - 1
}
return backwards ? stepBackForword : stepFoword
}
var currentValue1 = -4
let moveNearerToZero = chooseStepFunction(currentValue1 > 0)
// moveNearerToZero now refers to the nested stepForward() function
while currentValue1 != 0 {
print("\(currentValue1)... ")
currentValue1 = moveNearerToZero(currentValue1)
}
swift:函数Function
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 使用func来声明一个函数。通过在函数名后面添加包含参数列表的圆括号来执行函数。使用->来分离参数和返回类型。 练...
- 函数参数名称(Function Parameter Names) 函数参数都有一个外部参数名(external p...
- 一直在用OC写代码,对swift没有什么研究,一直想着不就一个新语言嘛,到用的时候顺便学习一下规则就行了,api那...
- 函数: 完成某个特定任务的代码块, 给代码起一个合适的名称称之为函数名称. 以后需要执行代码块只需要利用函数名称调...