Here's kinda' the basics. Notice that functions return a value, where as sub procedures don't. If you pass a variable to a procedure by reference, however, you can modify the variable's value in the calling procedure. This has the same effect as assigning the return value of a function to the variable.
Run the code by typing either CallProcedure or CallFunction in the Immediate Window and hitting enter. Set a break point on the procedure or function header and step through the code to see how they work. Watch the variable values change in the Locals Window.
Sub CallProcedure()
Dim intX As Integer, intY As Integer, intSum As Integer
intX = 2
intY = 2
'intSum us passed by reference
Call sCalculate(intX, intY, intSum)
Debug.Print intSum
End Sub
Sub CallFunction()
Dim intX As Integer, intY As Integer, intSum As Integer
intX = 2
intY = 2
' Assign functions return value to a variable
intSum = fCalculate(intX, intY)
Debug.Print intSum
End Sub
Sub sCalculate(ByVal intA As Integer, _
ByVal intB As Integer, _
ByRef intAddress As Integer)
intAddress = intA + intB
End Sub
Function fCalculate(ByVal intA As Integer, _
ByVal intB As Integer) As Integer
fCalculate = intA + intB
End Function
HTH,
Bob
|