Swift - 函数
简介
函数的定义与调用
关键字 函数名 参数 返回值
func sayHello (name: String) -> String {
// 函数实现
return "Hello," + name + "!"
}
函数参数与返回值
// 无参无返回值
func noParametersAndReturnValues() -> Void {
print("Hello,world!")
}
// 无参有返回值
func noParametersHasReturnValues() -> String {
return "value"
}
// 有参有返回值
func hasParametersAndReturnValues(var a: Int) -> Int {
return a++
}
// 有参无返回值
func hasParametersNoReturnValues(name: String) -> Void {
print("Hello,(name).")
}
函数返回值
// ([Int]) -> (Int,Int)
func maxAndMinNumber(numbers: [Int]) -> (max: Int,min: Int)? {
var maxNumber = numbers[0]
var minNumber = numbers[0]
for var i = 0; i < numbers.count; i++ {
maxNumber = numbers[i] > maxNumber ? numbers[i] : maxNumber
minNumber = numbers[i] < minNumber ? numbers[i] : minNumber
}
return (maxNumber,minNumber)
}
let (max,min) = maxAndMinNumber([1,3,2,4,5,9])!
print("max number is '(max)',min number is '(min)'.",terminator: "")
函数参数名称
// (String,Int,Int) -> void
func studentInfomation(name: String,age: Int,phone: Int) {
print("Student infomation: name is (name),age is (age),phone is (phone)",terminator: "")
}
studentInfomation("Edward",age: 19,phone: 13219038892)
指定外部参数名
// (String,Int) -> void
func studentInfomation(studentName name: String,studentAge age: Int,StudentPhone phone: Int) {
print("Student infomation: name is (name),terminator: "")
}
studentInfomation(studentName: "Edward",studentAge: 19,StudentPhone: 13219038892)
忽略外部参数名
// (String,_ age: Int,_ phone: Int) {
print("Student infomation: name is (name),19,13219038892)
默认参数值
func sumOfNumbes(a: Int,b: Int = 10) {
print(a + b)
}
sumOfNumbes(10) // 执行 10 + 默认值(10),输出 20
sumOfNumbes(10,b: 20) // 执行 10 + 20,输出 30
可变参数
func arithmeticMean(numbers: Double...) -> Double {
let count: Int = numbers.count
var sum: Double = 0
var average: Double = 0
for number in numbers {
sum += number
}
average = sum / Double(count)
return average
}
print("average is '(arithmeticMean(1,2,3,4,5))'.") // 输出 average is '3.0'.
常量参数和变量参数
func getResults(var a: Int) -> Int {
for index in 1...3 {
a += index
}
return a
}
print(getResults(10)) // 输出 16
输入输出参数
// 定义两个 In-Out 参数
func swap(inout a: Int,inout b: Int) {
// 在函数内部交换两个形参的值
let temp = a
a = b
b = temp
}
var a = 6
var b = 9
print("交换之前,a的值:(a), b的值:(b)")
swap(&a,&b)
print("交换之后,a的值:(a), b的值:(b)")
// 输出:
交换之前,a的值:6, b的值:9
交换之后,a的值:9, b的值:6
函数类型
// 1、无参无返回值,函数类型为:() -> void
func noParametersAndReturnValues() -> Void {
print("Hello,world!")
}
// 2、无参有返回值,函数类型为:() -> String
func noParametersHasReturnValues() -> String {
return "value"
}
// 3、有参有返回值,函数类型为:([Int]) -> (Int,minNumber)
}
// 4、有参无返回值,函数类型为:(String) -> void
func hasParametersNoReturnValues(name: String) -> Void {
print("Hello,(name).")
}
使用函数类型
// (Int,Int) -> Int
func sumOfNumber(a: Int,b: Int) -> Int {
return a + b
}
var mathFunction: (Int,Int) -> Int = sumOfNumber
print(mathFunction(2,3)) // 输出 5
函数类型作为参数类型
func sumOfNumber(a: Int,b: Int) -> Int {
return a + b
}
func printSumOfNumberResults(function: (Int,Int) -> Int,a: Int,b: Int) { let results = function(a,b) print("results is (results).",terminator: "") } printSumOfNumberResults(sumOfNumber,a: 100,b: 100)
函数类型作为返回类型
// 定义一个计算平方的函数
func square(val: Int) -> Int {
return Int(pow(Double(val),2.0))
}
// 定义一个计算立方的函数
func cube(val: Int) -> Int {
return Int(pow(Double(val),3.0))
}
// 定义一个计算阶乘的函数
func factorial(val: Int) -> Int {
var result = 1
for index in 2...val {
result *= index
}
return result
}
// 定义函数,该函数的返回值类型为:(Int) -> Int
func getMathFunc(type: String) -> (Int) -> Int {
switch (type) {
case "square":
return square
case "cube":
return cube
case "factorial":
return factorial
default:
print("输入有误!")
return square
}
}
// 调用 getMathFunc() 函数,程序将返回一个 (Int) -> Int 类型的函数
var mathFunc = getMathFunc("square") // 得到 square 函数
print(mathFunc(5))
mathFunc = getMathFunc("cube") // 得到 cube 函数
print(mathFunc(5))
mathFunc = getMathFunc("factorial") // 得到 factorial 函数
print(mathFunc(5))
嵌套函数
// 定义函数,该函数的返回值类型为:(Int) -> Int
func getMathFunc(type: String) -> (Int) -> Int {
// 定义一个计算平方的函数
func square(val: Int) -> Int {
return Int(pow(Double(val),2.0))
}
// 定义一个计算立方的函数
func cube(val: Int) -> Int {
return Int(pow(Double(val),3.0))
}
// 定义一个计算阶乘的函数
func factorial(val: Int) -> Int {
var result = 1
for index in 2...val {
result *= index
}
return result
}
// 该函数的返回值是嵌套函数
switch (type) {
case "square":
return square
case "cube":
return cube
case "factorial":
return factorial
default:
print("输入有误!")
return square
}
}
// 调用 getMathFunc() 函数,程序将返回一个 (Int) -> Int 类型的函数
var mathFunc = getMathFunc("square") // 得到 square 函数
print(mathFunc(5))
mathFunc = getMathFunc("cube") // 得到 cube 函数
print(mathFunc(5))
mathFunc = getMathFunc("factorial") // 得到 factorial 函数
print(mathFunc(5))
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |