티스토리 뷰

반응형

 

Swift Docs > Functions 을 읽다가 자주 혼용되는 용어들을 정확하게 잘 구분해서 쓰는 점이 인상깊었다.

나도 잘 리마인드해서 정확한 용어를 쓰도록 해야겠다. 


 

# parameter 와 argument

parameter (매개변수) 는 함수의 정의에 포함되는 변수.

argument (전달인자) 는 함수를 call 할 때 전달하는 실제값.

 

스택오버플로우의 이 설명이 가장 간단명료한 것 같아서 가져옴

 

- Parameter is variable in the declaration of function.

- Argument is the actual value of this variable that gets passed to function. 

 

 

https://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter

 

 

 

아래는 Swift Docs > Functions 의 내용이다. 

 

To use a function, you “call” that function with its name and pass it input values (known as arguments) that

match the types of the function’s parameters

A function’s arguments must always be provided in the same order as the function’s parameter list.

 

 

 

# argument label 과 argument value

 

Swift Docs 에서는 argument label, argument value 를 구분해서 사용하고 있었다. 

 

You call the greet(person:alreadyGreeted:) function by passing it both a String argument value labeled 

person and a Bool argument value labeled alreadyGreeted in parentheses, separated by commas.

 

func greet(person: String, alreadyGreeted: Bool) -> String {
    if alreadyGreeted {
        return greetAgain(person: person)
    } else {
        return greet(person: person)
    }
}
print(greet(person: "Tim", alreadyGreeted: true))
// Prints "Hello again, Tim!"

 

You call the greet(person:) function by passing it a String value after the person argument label, such as greet(person: "Anna"). 

print(greet(person: "Anna"))
// Prints "Hello, Anna!"
print(greet(person: "Brian"))
// Prints "Hello, Brian!"

 

그리고 코드 주석 몇군데에 argument value 라고 명확하게 표현하고 있다.

func someFunction(_ firstParameterName: Int, secondParameterName: Int) {
    // In the function body, firstParameterName and secondParameterName
    // refer to the argument values for the first and second parameters.
}
someFunction(1, secondParameterName: 2)

 

 

# parameter name 과 argument label

func someFunction(argumentLabel parameterName: Int) {
    // In the function body, parameterName refers to the argument value
    // for that parameter.
}

 

Each function parameter has both an argument label and a parameter name.

The argument label is used when calling the function; 

each argument is written in the function call with its argument label before it. 

 

The parameter name is used in the implementation of the function. 

By default, parameters use their parameter name as their argument label.

 

 

 

# 그외 (implicit return)

return keyword를 생략하는 것을 implicit return 이라고 부른다.

(single expression body인 function, closure 또는 property getter 에서 implicit return을 사용가능함)

 

 

 

 

반응형

'🍏 > Swift' 카테고리의 다른 글

[Swift] Property Wrapper  (0) 2022.01.06
[Swift] Autoclosure 개념과 활용사례  (0) 2021.12.30
[Swift] Concurrency  (2) 2021.12.14
[Swift] Strings and Characters > Unicode  (0) 2021.12.12
[Swift] Substring  (0) 2021.12.12
댓글