Files
SwiftStudy/04 函数.playground/Contents.swift
2021-12-14 09:36:26 +08:00

227 lines
5.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import UIKit
var greeting = "Hello, playground"
//: [Previous](@previous)
import Darwin
import Foundation
//: ##
/*:
:
func (: , ...) -> {}
- letlet
*/
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
//: ###
///
///
/// -Parameters v1
///
func addNumber(v1: Int, v2: Int) -> Int {
v1 + v2
}
//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)