swift - 매개변수(parameter)에 for가 들어간 경우

Boomerang·2021년 8월 9일
0

IOS

목록 보기
2/5
post-thumbnail

코드를 보자

func greeting(for person: String) -> String {
    "Hello, " + person + "!"
}
print(greeting(for: "Dave"))
// Prints "Hello, Dave!"

func anotherGreeting(for person: String) -> String {
    return "Hello, " + person + "!"
}
print(anotherGreeting(for: "Dave"))
// Prints "Hello, Dave!"

Functions With an Implicit Return

위는 impicit Return이라고 한다.
더 짧게 return을 쓰지않고 return 할 수 있다.

implicit : 함축적인
explicit : 명백한


Implicit Return

A function is returned values without using the return keyword, it's called an implicit return. You must use an implicit return in a concise body.



Functions With an Implicit Return

If the entire body of the function is a single expression, the function implicitly returns that expression. For example, both functions below have the same behavior:

The entire definition of the greeting(for:) function is the greeting message that it returns, which means it can use this shorter form. The anotherGreeting(for:) function returns the same greeting message, using the return keyword like a longer function. Any function that you write as just one return line can omit the return.

참고

profile
Hello World

0개의 댓글