function


Functions - The Swift Programming Language (Swift 5.6)
Functions are self-contained chunks of code that perform a specific task.
*関数は、特定のランダムな操作を実行するコードブロックです.
use a tuple type as the return type for a function to return multiple values as part of one compound return value.
=複数の値を返す
accessed with dot syntax to retrieve the minimum and maximum found values
dot syntaxを使用してtuple戻り値にアクセスできます
func minMax(array: [Int]) -> (min: Int, max: Int)
.
.
.
let bounds = minMax(array: [8, -6, 2, 109, 3, 71])
print("min is \(bounds.min) and max is \(bounds.max)")
パラメータにデフォルト値を割り当てる
func someFunction(parameterWithoutDefault: Int, parameterWithDefault: Int = 12)
Place parameters that don’t have default values at the beginning of a function’s parameter list, before the parameters that have default values. Parameters that don’t have default values are usually more important to the function’s meaning—writing them first makes it easier to recognize that the same function is being called, regardless of whether any default parameters are omitted.
=エラー値のパラメータが後ろにある
◆理由:デフォルト値がないパラメータは通常より重要で、識別しやすく、デフォルトパラメータを省略しやすい
variadic parameter accepts zero or more values of a specified type.
The values passed to a variadic parameter are made available within the function’s body as an array of the appropriate type.
⇒ variadic parameter 0個以上の値をパラメータとして表すことができます
関数の内部で配列として使用
func arithmeticMean(_ numbers: Double...) -> Double {
    var total: Double = 0
    for number in numbers {
        total += number
    }
    return total / Double(numbers.count)
}
Function parameters are constants by default.
If you want a function to modify a parameter’s value, and you want those changes to persist after the function call has ended, define that parameter as an in-out parameter
instead.
パラメータは定数で変更できません
パラメータ値はin-outパラメータで変更できます
In-out parameters can’t have default values, and variadic parameters can’t be marked as  inout .
=in-outパラメータにデフォルト値は使用できません.変数パラメータでは使用できません.
func swapTwoInts(_ a: inout Int, _ b: inout Int) {
    let temporaryA = a
    a = b
    b = temporaryA
}
var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
print("someInt is now \(someInt), and anotherInt is now \(anotherInt)")
// Prints "someInt is now 107, and anotherInt is now 3"
in-out parameters aren’t the same as returning a value from a function.
In-out parameters are an alternative way for a function to have an effect outside of the scope of its function body.
=returnは通常、外部に影響を与える一意の値です.
戻り値とは異なり、in-outパラメータは外部に影響します(理由:パラメータとして渡される値が変更されました)
function type
var mathFunction: (Int, Int) -> Int = addTwoInts
print("Result: \(mathFunction(2, 3))")
// Prints "Result: 5"