각 데이터 조각은 인수를 사용하여 프로시저에 전달됩니다. 인수는 프로시저에 전달할 데이터의 자리 표시자 역할을 합니다. 인수의 이름을 적합한 변수 이름으로 지정할 수 있습니다. Sub 명령문 또는 Function 명령문을 사용하여 프로시저를 생성할 때는 프로시저 이름 뒤에 괄호가 포함되어야 합니다. 모든 인수는 쉼표로 구분되는 이러한 괄호 안에 배치됩니다. 예를 들어, 다음 예에서 fDegrees는 변환을 위해 Celsius 함수로 전달되는 값에 대한 자리표시자입니다.
다음 예는 프로시저에서 데이터 가져오기를 사용하는 방법을 설명합니다.
예 1:
Function Celsius(fDegrees) Celsius = (fDegrees - 32) * 5 / 9 End Function
프로시저에서 데이터를 가져오려면 Function을 사용해야 합니다. Function 프로시저는 값을 반환할 수 있지만, Sub 프로시저는 반환할 수 없습니다.
예 2:
Function Celsius(fDegrees)
Celsius = (fDegrees - 32) * 5 / 9 ' Converts Fahrenheit to Celsius.
End Function
Sub ConvertTemp()
Dim temp, celsiusTemp
temp = 70
celsiusTemp = Celsius(temp) ' Calls the function and stores the result.
'celsiusTemp has the converted temperature.
End Sub
Call ConvertTemp()
예 3:
Function Add(a, b)
Add = a + b ' Returns the sum of a and b.
End Function
Sub ShowAddition()
Dim result
result = Add(5, 10) ' Calls the function and stores the result.
'Here result is 15
End Sub
Call ShowAddition()
예 4:
Function GetGreeting(name, timeOfDay)
If timeOfDay = "Morning" Then
GetGreeting = "Good morning, " & name & "!"
ElseIf timeOfDay = "Afternoon" Then
GetGreeting = "Good afternoon, " & name & "!"
ElseIf timeOfDay = "Evening" Then
GetGreeting = "Good evening, " & name & "!"
Else
GetGreeting = "Hello, " & name & "!"
End If
End Function
Sub ShowGreeting()
Dim name, timeOfDay, greeting
name = "MyName"
timeOfDay = "Morning"
greeting = GetGreeting(name, timeOfDay)
'Print greeting here
End Sub
Call ShowGreeting()