diff --git a/SwiftStudy.playground/Pages/04 函数.xcplaygroundpage/Contents.swift b/SwiftStudy.playground/Pages/04 函数.xcplaygroundpage/Contents.swift new file mode 100644 index 0000000..586d5d7 --- /dev/null +++ b/SwiftStudy.playground/Pages/04 函数.xcplaygroundpage/Contents.swift @@ -0,0 +1,48 @@ +//: [Previous](@previous) + +//: ## 函数 +/*: + 函数的定义: + func 函数名称 (形参: 形参类型, ...) -> 函数返回值 {} + - 形参默认是let,也只能是let + */ +func sum(v1: Int, v2: Int) -> Int { + return v1 + v2 +} + + +//: ### 无返回值的函数 +func sayHello() -> Void {} +func sayHello1() -> () {} +func sayHello2() {} + +//: ### 隐式返回值 +// 如果整个函数体是一个单一表达式,那么函数会隐式返回这个表达式 +func sum0(v1: Int, v2: Int) -> Int { + v1 + v2 +} + +//: ### 返回元组(实现多返回值) +func caculate(v1: Int, v2: Int) -> (sum: Int, difference: Int, average: Int) { + let sum = v1 + v2 + + return (sum, v1 - v2, sum >> 1) +} + +let result = caculate(v1:20, v2:10) +result.sum + +//: ### 函数文档注释 +/// 求和 +/// +/// +func addNumber(v1: Int, v2: Int) -> Int { + v1 + v2 +} + +addNumer(v1: 20, v2: 30); + +//: ###参数标签 + + +//: [Next](@next) diff --git a/SwiftStudy.playground/contents.xcplayground b/SwiftStudy.playground/contents.xcplayground index 9e18224..0b8b36b 100644 --- a/SwiftStudy.playground/contents.xcplayground +++ b/SwiftStudy.playground/contents.xcplayground @@ -4,5 +4,6 @@ + \ No newline at end of file diff --git a/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate index 6ee4b49..2d8133d 100644 Binary files a/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate and b/SwiftStudy.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate differ