0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Swift】関数の引数ラベル

Posted at

目次

1.関数の引数ラベルとは?
2.実例
3.おわりに

1. 関数の引数ラベルとは?

引数ラベルと聞いて皆さんはパッとイメージできますか?
私は、こんな記法Swiftで初めて出会いました。
実は、Swiftの関数の引数にはラベルが付けれるのです。しかも関数の中では使用せず、関数を呼び出すときにのみ使用されます。
この記法のことを引数ラベルと言います。

2. 実例

引数ラベルなし
func greeting(name: String, country: String) {
    print("Hello \(name) from \(country) !!")
}

greeting(name: "Tommy", country: "UK")
引数ラベルあり
func greeting(firstname name: String, from country: String) {
    print("Hello \(name) from \(country) !!")
}

greeting(firstname: "Tommy", from: "UK")

nameと言われてもfirstnameを書けば良いのか?familynameを書けば良いのかわからないですよね?
引数ラベルを使用することによって、関数を使用する際により分かりやすく可読性を高める目的で使用されます。(個人的には、引数名で可読性を担保してよ〜て感じです笑)

3. おわりに

皆さんは、関数ラベルの存在意義わかりましたでしょうか?
私は正直あまり感じていないですが、Swiftにはこんな記法があるんだ!くらいに思っておきます。

本記事を読んでいただきありがとうございました。
目指せ脱初学者で頑張りましょ〜〜

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?