美文网首页
Swift语言中函数

Swift语言中函数

作者: 玻璃瓶外的水 | 来源:发表于2016-05-27 15:47 被阅读13次

    swift语言中函数是一个重要的概念,今天主要谈论下函数的相关知识:

    函数基础:

    先看一段代码:


    func printName() {

    print("My name is Galloway")

    }

    printName()

    这段代码前三行是声明了一个函数,最后一行是执行这段函数,声明函数的时候使用关键字func,之后是函数名,在花括号中是整个函数体。

    下面再看一段代码:

    func printMultipleOf(multiplier: Int, andValue: Int) {

    print("\(multiplier) * \(andValue) = \(multiplier * andValue)")

    }

    printMultipleOf(4, andValue: 2)

    很明显,这段代码中也声明了一个函数,但区别是这个函数带有2个参数,函数中的参数可以称为参数列表,参数中的格式为:参数名:参数类型

    上面这段代码同样可以改为:

    func printMultipleOf(multiplier: Int, and andValue: Int) {

    print("\(multiplier) * \(andValue) = \(multiplier * andValue)")

    }

    printMultipleOf(4, and: 2)

    这里第二个参数改为:参数名1 参数名2: 参数类型。这种写法表示参数名1用于外部调用函数时使用,参数名2用于函数内部实现时使用,可以有利于函数的复用。

    继续看一段代码:

    func printMultipleOf(multiplier: Int, _ andValue: Int) {

    print("\(multiplier) * \(andValue) = \(multiplier * andValue)")

    }

    printMultipleOf(4, 2)

    这里第二个参数设置为:_ 参数名2: 参数类型。这样就表示调用这个函数的时候不需要指定名字。

    func printMultipleOf(multiplier: Int, _ andValue: Int = 1) {

    print("\(multiplier) * \(andValue) = \(multiplier * andValue)")

    }

    printMultipleOf(4)

    printMultipleOf(4,2)

    这段代码设置了第二个参数的默认值为1.

    上面这段代码是没有返回值的,下面看下有返回值的代码:

    func multiply(number: Int, by byValue: Int) -> Int {

    return number * byValue

    }

    let mulresult = multiply(4, by: 2)

    返回值的类型在 ->后,这段函数的返回值类型是Int型,return后面是返回值。

    下面再看一段代码:

    func multiplyAndDivide(number: Int, by byValue: Int) -> (multiply: Int, divide: Int) {

    return (number * byValue, number / byValue)

    }

    let mulresult = multiplyAndDivide(4, by: 2)

    let multiply = mulresult.multiply

    let divide = mulresult.divide

    这段代码返回了一个元组(tuples)。

    继续看一段代码:

    unc incrementAndPrint(inout value: Int) {

    value += 1

    print(value)

    }

    var value = 5

    incrementAndPrint(&value)

    这里使用inout关键字来进行引用参数传递。

    函数同样可以作为变量来使用,和基本的数据类型一样

    看下面这段代码:

    func add(a:Int, _ b: Int) -> Int {

    return a + b

    }

    var function: (Int, Int) -> Int = add

    let result = function(4,2)

    这段代码就是将函数作为变量,使用起来是和基本的数据类型一致。

    func substract(a: Int, _ b: Int) -> Int {

    return a-b

    }

    function = substract

    result = function(4,2)

    因为类型是一致的,同样可以把subtract赋值给function。

    func printResult(function: (Int,Int) -> Int, _ a: Int, _ b: Int) {

    let result = function(a,b)

    print(result)

    }

    printResult(add, 8, 7)

    这里function函数作为printResult函数的一个参数,输出的结果为15。


    下面是两个练习:

    1、

    func isNumberDivisible(number: Int, by byNumber: Int) -> Bool {

    if number % byNumber == 0 {

    return true

    } else {

    return false

    }

    }

    func isPrime(number: Int) -> Bool {

    if number >= 0 {

    for i in 2...Int(sqrt(Double(number))) {

    if isNumberDivisible(number, by: i) {

    return false

    }

    }

    return true

    } else {

    return false

    }

    }

    isPrime(18653)

    判断一个数是否是素数

    2、

    func fibonacci(number: Int) -> Int {

    if number == 1 || number == 2 {

    return 1

    } else {

    return fibonacci(number-2) + fibonacci(number-1)

    }

    }

    fibonacci(1)

    fibonacci(2)

    fibonacci(3)

    fibonacci(4)

    fibonacci(5)

    fibonacci(9)

    fibonacci(19)

    fibonacci(1876)

    计算Fibonacci数列


    相关文章

      网友评论

          本文标题:Swift语言中函数

          本文链接:https://www.haomeiwen.com/subject/ygxfdttx.html