Getting Data Into and Out of Procedures

Each piece of data is passed into your procedures using an argument. Arguments serve as placeholders for the data you want to pass into your procedure. You can name your arguments any valid variable name. When you create a procedure using either the Sub statement or the Function statement, parentheses must be included after the name of the procedure. Any arguments are placed inside these parentheses, separated by commas. For example, in the following example, fDegrees is a placeholder for the value being passed into the Celsius function for conversion.

The following example illustrates the use of the getting data out of the procedure:

Example 1:

Function Celsius(fDegrees)
   Celsius = (fDegrees - 32) * 5 / 9
End Function

To get data out of a procedure, you must use a Function. Remember, a Function procedure can return a value; a Sub procedure can't.

Example 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()

Example 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()

Example 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()