0
1

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 3 years have passed since last update.

暗黙のリターン

Posted at
var userName = "Watanabe"

func makeMassage(user: String) -> String {
    "Hello, \(user)"
}

makeMassage(user: userName)

上のサンプルコードは、戻り値がある関数なのに、returnをつけていません。
関数内の実装が戻り値の返却のみの時だけ、暗黙的なreturnができます。

func nonMakeMassage(user: inout String) -> String {
    user = "Tanaka"
    "Hello, \(user)"
}

nonMakeMassage(user: &userName)

当たり前ですが上のコードだと

Missing return in a function expected to return 'String'; did you mean to return the last expression?

と怒られました。

参考

[増補改訂第3版]Swift実践入門 ── 直感的な文法と安全性を兼ね備えた言語 (WEB+DB PRESS plusシリーズ) 

0
1
0

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?