본문 바로가기
언어 참조

명명된 인수 및 선택적 인수 이해

2023. 8. 3.

이 글은 도움말을 번역한 글입니다.

Sub 또는 Function 프로시저를 호출할 때 프로시저의 정의에 나타나는 순서대로 인수를 위치별로 제공하거나 위치에 관계없이 이름으로 인수를 제공할 수 있습니다.

예를 들어, 다음 Sub 프로시저는 세 개의 인수를 받습니다.

Sub PassArgs(strName As String, intAge As Integer, dteBirth As Date) 
 Debug.Print strName, intAge, dteBirth 
End Sub

다음 예제와 같이 쉼표로 구분된 인수를 올바른 위치에 제공하여 이 프로시저를 호출할 수 있습니다.

PassArgs name, age, birthDate

Sub CallPassArgs()
    Dim name As String
    Dim age As Integer
    Dim birthDate As Date
    
    ' 변수에 값을 할당합니다.
    name = "John Doe"
    age = 30
    birthDate = #8/15/1992#
    
    ' PassArgs 서브 프로시저 호출
    Call PassArgs(name, age, birthDate)
End Sub
Sub PassArgs(strName As String, intAge As Integer, dteBirth As Date)
 Debug.Print strName, intAge, dteBirth
End Sub

' 출력 결과
' John Doe       30           8/15/1992

명명된 인수를 제공하고 각 인수를 쉼표로 구분하여 이 프로시저를 호출할 수도 있습니다.

Sub CallPassArgs()
    Dim name As String
    Dim age As Integer
    Dim birthDate As Date
    
    ' 변수에 값을 할당합니다.
    name = "John Doe"
    age = 30
    birthDate = #8/15/1992#
    
    ' PassArgs 서브 프로시저 호출
    PassArgs intAge:=29, dteBirth:=#2/21/1969#, strName:="Mary"
End Sub
Sub PassArgs(strName As String, intAge As Integer, dteBirth As Date)
 Debug.Print strName, intAge, dteBirth
End Sub

' 출력 결과
' Mary           29           2/21/1969

명명된 인수는 인수 이름 뒤에 콜론과 등호(:=)가 따라오며, 인수 값으로 구성됩니다.

PassArgs intAge:=29, dteBirth:=#2/21/1969#, strName:="Mary"


명명된 인수는 특히 선택적 인수가 있는 프로시저를 호출할 때 유용합니다. 이를 사용하면 누락된 위치 인수를 나타내기 위해 쉼표를 사용할 필요가 없어집니다. 또한, 전달한 인수와 생략한 인수를 쉽게 추적할 수 있게 됩니다.

명명된 인수를 사용하지 않는 경우

Function AddNumbers(Optional a As Long = 0, Optional b As Long = 0, Optional c As Long = 0) As Long
    AddNumbers = a + b + c
End Function
Sub Main()
    Dim result As Long
    result = AddNumbers(5, , 7)
    MsgBox "Result: " & result
End Sub

명명된 인수를 사용하는 경우

Function AddNumbers(Optional a As Long = 0, Optional b As Long = 0, Optional c As Long = 0) As Long
    AddNumbers = a + b + c
End Function
Sub Main()
    Dim result As Long
    result = AddNumbers(a:=5, c:=7)
    MsgBox "Result: " & result
End Sub


선택적 인수는 프로시저 정의에서 Optional 키워드로 지정될 수 있으며, 선택적 인수에 대한 기본값도 정의할 수 있습니다.

Function CalculateRectangleArea(Optional ByVal length As Double = 5, Optional ByVal width As Double = 3) As Double
    CalculateRectangleArea = length * width
End Function
Sub TestRectangleArea()
    Dim area1 As Double
    Dim area2 As Double
    Dim area3 As Double
    
    ' Case 1: Providing both length and width
    area1 = CalculateRectangleArea(length:=10, width:=6)
    Debug.Print "Area 1: " & area1   ' Output: Area 1: 60
    
    ' Case 2: Providing only the length, using default width
    area2 = CalculateRectangleArea(length:=8)
    Debug.Print "Area 2: " & area2   ' Output: Area 2: 24
    
    ' Case 3: Using default length and width
    area3 = CalculateRectangleArea()
    Debug.Print "Area 3: " & area3   ' Output: Area 3: 15
End Sub

선택적 인수가 있는 프로시저를 호출할 때 선택적 인수를 지정할지 여부를 선택할 수 있습니다. 선택적 인수를 지정하지 않으면 기본값이 있는 경우 기본값이 사용됩니다. 기본값을 지정하지 않으면 지정된 유형의 변수에 대한 인수가 사용됩니다.

Sub OptionalArgs(strState As String, Optional varRegion As Variant, _
    Optional varCountry As Variant = "USA")
    If IsMissing(varRegion) And IsMissing(varCountry) Then
        Debug.Print strState
    ElseIf IsMissing(varCountry) Then
        Debug.Print strState, varRegion
    ElseIf IsMissing(varRegion) Then
        Debug.Print strState, varCountry
    Else
        Debug.Print strState, varRegion, varCountry
    End If
End Sub

Sub CallOptionalArgs()
  
    OptionalArgs "New York"
    
    OptionalArgs "California", "West"
    
    OptionalArgs "Texas", , "USA"
    
    OptionalArgs "Ontario", "East", "Canada"
    
    OptionalArgs strState:="Chicago", varCountry:="USA"
    
    OptionalArgs varRegion:="North", strState:="Alaska"
    
End Sub

도움말 출처

Understanding named arguments and optional arguments (VBA)

Office VBA reference topic

learn.microsoft.com

'언어 참조' 카테고리의 다른 글

인수를 효율적으로 전달하기  (0) 2023.08.04
매개변수 배열 이해  (0) 2023.08.04
배열 선언 및 사용  (0) 2023.08.01
속성을 설정할 때 코드 실행  (0) 2023.08.01
Property 프로시저 호출  (0) 2023.08.01