The techniques for calling procedures vary, depending on the type of procedure, where it’s located, and how it’s used in your application. The following sections describe how to call Sub and Function procedures.
Calling Sub Procedures
A Sub procedure differs from a Function procedure in that a Sub procedure cannot be called by using its name within an expression. A call to a Sub is a stand-alone statement. Also, a Sub does not return a value in its name as does a function. However, like a Function, a Sub can modify the values of any variables passed to it.
There are two ways to call a Sub procedure:
' Both of these statements call a Sub named MyProc.
Call MyProc (FirstArgument, SecondArgument)
MyProc FirstArgument, SecondArgument
It should be noted that when you use the Call syntax, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments.
Function Procedures
Usually, you call a function procedure you’ve written yourself the same way you call an intrinsic Visual Basic function like Abs, that is, by using its name in an expression:
' All of the following statements would call a function named ToDec.
Print 10 * ToDec
X = ToDec
If ToDec = 10 Then Debug.Print "Out of Range"
It’s also possible to call a function just like you would call a Sub procedure. The following statements both call the same function:
Call Year(Now)
Year Now
When you call a function this way, Visual Basic throws away the return value.
No comments:
Post a Comment