関数の引数に呼び出し元からの変数を渡して、関数内でその引数の値を変更して返すのにはinout
を使う。引数として渡す変数には”&”を前につける。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func plusFive(inout num:Int){ | |
num += 5 | |
} | |
var num : Int = 3 | |
println(num) //3 | |
plusFive(&num) | |
println(num) //8 |
[結果]
3
8