swift函数_10_swift中函数基本使用

//: Playground - noun: a place where people can play

import UIKit

//1.函数的定义
// func 函数名(参数1名:参数类型, 参数2名:参数类型)-> 返回值类型{

//函数体内容
//}
func tenTimes(times : Int) -> Int {
    
    return times * 10
}

//2.函数的调用
var parameters = tenTimes(2)

for i in 0...9 {
    
    parameters = tenTimes(i)
}

//3.多重参数函数
func bulidASentence(name : String, age : Int, weight : Int) -> String {
    
    let sentence = "(name) is (age) year's old and (weight)kg."
    
    return sentence
    
    
}
//一个函数有多个参数时,第一个参数可以省略参数,从第二个参数起不能省略
bulidASentence("孙悟空", age: 50000, weight: 100)

//4.无参数时,括号不能省略
func sayHello() -> String {
    
    return "hello"
}

func sayHello2(_ : Void) -> String {
    
    return "hello"
}

sayHello()

//5.无返回值
func sayByebye(name : String) -> () {
    print("ByeBye! (name)")
    
}

sayByebye("Tom")

func sayByebye2(name : String) {
    print("ByeBye! (name)")
    
}
时光见证了成长,还很无知,我想一点点幼稚转为有知!
原文地址:https://www.cnblogs.com/foreveriOS/p/5558897.html