【Swift】外部引数名を使って関数を定義する

関数内で使用する引数名以外に、関数の呼び出し元で引数の意味を分かりやすくするために外部引数名をつけることができる。

関数内の引数名と外部引数名が同じ場合は”#”を関数内引数名の前につけることで省略することができる。

//外部引数名を定義
func convertSeconds(hour h:Int , minutes m:Int) -> Int{
return 60 * (60 * h + m )
}
//外部引数名と引数名が同じ場合
func convertSeconds2(#hour:Int , #minutes:Int) -> Int{
return 60 * (60 * hour + minutes )
}
//
convertSeconds(hour: 3, minutes: 25) //12,300
convertSeconds2(hour: 3, minutes: 25) //12,300
view raw gistfile1.swift hosted with ❤ by GitHub

You must be logged in to post a comment.

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