Swift - 26 - 函数的基础写法

时间:2023-03-09 06:01:01
Swift - 26 - 函数的基础写法
//: Playground - noun: a place where people can play

import UIKit

// 无参无返回
// -> Void可以省略不写, 或者写成(), 因为返回值为空本质是一个空的元组
func run() -> Void // ()
{
print("I'm running")
}
run() // 无参有返回
func sayWelcome() -> String
{
return "Welcome to play Swift!"
}
print(sayWelcome()) // 有参有返回
func sayHello(name:String?) -> String
{
let result = "Hello," + (name ?? "Guest") + "!"
return result
} var nickName:String?
print(sayHello(nickName))