【Swift】関数の引数の値を変更する – inout

関数の引数に呼び出し元からの変数を渡して、関数内でその引数の値を変更して返すのにはinoutを使う。引数として渡す変数には”&”を前につける。

func plusFive(inout num:Int){
num += 5
}
var num : Int = 3
println(num) //3
plusFive(&num)
println(num) //8
view raw gistfile1.swift hosted with ❤ by GitHub

[結果]

3
8

You must be logged in to post a comment.

Proudly powered by WordPress   Premium Style Theme by www.gopiplus.com