函数乡相关知识点

This commit is contained in:
2021-12-04 14:51:13 +08:00
parent e4cf88c175
commit 725918c297
2 changed files with 175 additions and 1 deletions

View File

@@ -1,5 +1,8 @@
//: [Previous](@previous)
import Darwin
import Foundation
//: ##
/*:
:
@@ -35,14 +38,185 @@ result.sum
//: ###
///
///
/// -Parameters v1
///
func addNumber(v1: Int, v2: Int) -> Int {
v1 + v2
}
addNumer(v1: 20, v2: 30);
//addNumer(v1: 20, v2: 30);
//: ###
// at time
// 使_
func goToWork(at time: String) {
print("this time is \(time)")
}
goToWork(at: "08:00")
//
// name
func check(name: String = "nobody", age: Int, job: String = "none") {
print("name = \(name), age = \(age), job = \(job)")
}
//
//
func test(_ first: Int = 10, middle: Int, _ last: Int = 30) {}
//
/*:
:
- 1
-
*/
func sum(_ numbers: Int...) -> Int {
var total = 0
for number in numbers {
total += number
}
return total
}
// str
func test(_numbers: Int..., str: String, _ other: String) {}
// swiftprint
//public func print(<#T##Any#>, separator: <#T##String#>, terminator: <#T##String#>)
//: ####(In-Out Parameter)
/*:
inout
- inout
- inout
- inout
- inout
*/
// inout
// inout
var number = 10
func add(_ num: inout Int) {
num += 20
}
add(&number)
print(number)
//: ### Function Overload
/*:
:
-
- || ||
:
-
- 使
- 使
*/
func sum(v1: Int, v2: Int) -> Int {}
func sum(v1: Int, v2: Int, v3: Int) -> Int {} //
func sum(_ v1: Int, _ v2: Int) -> Int {} //
// 使
func sum(v1: Int, v2: Int) -> Int {
v1 + v2
}
func sum(v1: Int, v2: Int, v3: Int) -> Int {
v1 + v2 + v3
}
sum(v1: 10, v2: 20)
//: ####
/*:
*/
// () -> Void () -> ()
func test() {}
// (Int, Int) -> Int
func sum(a: Int, b: Int) -> Int {
a + b
}
//
var fn: (Int, Int) -> Int = sum
fn(2, 3) //
//
func sum(v1: Int, v2: Int) -> Int {
v1 + v2
}
func difference(v1: Int, v2: Int) -> Int {
v1 - v2
}
func printResult(_ mathFn: (Int, Int) -> Int, _ a: Int, _ b: Int) {
print("Result: \(mathFn(a, b))")
}
printResult(sum, 5, 2)
printResult(difference, 5, 2)
// ,Higher-Order Function
func next(_ input: Int) -> Int {
input + 1
}
func previous(_ input: Int) -> Int {
input - 1
}
// (Int) -> Int
func forward(_ forword: Bool) -> (Int) -> Int {
forword? next: previous
}
//: #### typealias
typealias Byte = Int8
typealias Short = Int16
typealias Long = Int64
typealias Date = (year: Int, month: Int, day: Int)
func test (_ date: Date) {
print(date.0)
print(date.year)
}
test((2011,11,1))
typealias intFn = (Int, Int) -> Int
//: ### (Nested Function)
/*:
:(Nested Function)
-
*/
//
func forward(_ forword: Bool) -> (Int) -> Int {
func next(_ input: Int) -> Int {
input + 1
}
func previous(_ input: Int) -> Int {
input - 1
}
forword? next: previous
}
forward(true)(3)
forward(false)(3)
//: [Next](@next)