Sub Sample()
Dim a As Integer, b As Integer
MsgBox test1(10, 5) '50が返ります
MsgBox test1(10) '20が返ります
a = 10
b = 10
MsgBox test2(a, b) '40が返ります
MsgBox a '10が返ります
MsgBox b '20が返ります
MsgBox test3(1, 2, 3) '6が返ります
MsgBox test3(1, 2) '3が返ります
MsgBox test4(10, 2) '20が返ります
MsgBox test4(10) '50が返ります
End Sub
Function test1(arg1, Optional arg2)
If IsMissing(arg2) Then '第2引数arg2は省略可能
test1 = arg1 * 2
Else
test1 = arg1 * arg2
End If
End Function
Function test2(ByVal arg1, ByRef arg2)
arg1 = arg1 * 2 '値渡し
arg2 = arg2 * 2 '参照渡し
test2 = arg1 + arg2
End Function
Function test3(ParamArray args())
Dim buf As Integer, i As Integer 'argsは省略可能な配列
For i = 0 To UBound(args)
buf = buf + args(i)
Next i
test3 = buf
End Function
Function test4(arg1, Optional arg2 = 5)
test4 = arg1 * arg2 'arg2が省略された場合の既定値は5
End Function