example 2. example 3 sub procedures versus function procedures a sub procedure is a procedure that...
Post on 19-Dec-2015
226 views
TRANSCRIPT
Sub Procedures versus Function Procedures
A Sub Procedure is a procedure that performs an action
A Function Procedure may perform an action, but it also returns a value (the return value) to the point in
a procedure from which it was called
Creating a ‘Function Procedure’
• 2 ways to create a Function Procedure
• [1] simply type in the procedure definition in the code window
• [2] use the tools / add procedure menu command
Same way as creating a sub procedures
Creating a ‘Function Procedure’
The Function statement declares the name, arguments, and code that form the body of a function, as well as the data type of the value returned
[Private | Public] Function Name [(arguments)] [As Data Type]
statements
End Function
Calling a ‘Function Procedure’
A Function Procedure is almost always used as part of an expression in a statement
ReturnedValue = FunctionName( )
Remember the use of system-defined functions
stUserName = Inputbox(“Enter User Name”, “User Input”)
Passing ARGUMENTS to Procedures
At times there may be a need to use the value of a variable in one procedure and also in a second procedure, that is called from the
first
The variable could be declared at Module Level, but this makes it visible to all other procedures, and the scope of variables should
always be kept as narrow as possible
In order to keep the scope of the variable as narrow as possible, consider declaring the variable as Local and passing it (as an
argument) to any Called Procedures
When passing arguments to procedures the ProcedureName has an argument inside the Parenthesis. This specifies that when the
procedure is called, an argument must be supplied
When a Procedure definition names an argument, any call to that procedure must supply an argument. Also, the argument must be of the same Data Type in both the definition and call locations
The Name of the argument does not have to be the same in both locations
When multiple arguments are specified in both the Procedure header and the Call of the procedure, the number of arguments,
their sequence, and their data types must match in both locations
Passing ARGUMENTS to Procedures
In a procedure, the argument list you enter establishes the number of arguments, their data type, and their
sequence. When using multiple arguments, the sequence of the arguments is critical, just as when you
see the predefined VB functions
Very Important to Remember
Sub Procedures and Function ProceduresA Comparison
The two examples, one using Sub Procedure, and the second using Function Procedures, do exactly the same thing in relation to the outputted results,
however, they are coded differently
Both programs are checking to see if the user has entered a value greater than the minimum allowable loan. The value entered by the user is displayed in a text box, and whether the loan amount is “Valid” or
an “Invalid” request is also printed to the form
Scope of Variables: General Sub Procedures and
Function Procedures
The user clicks on the command button
Example 2
Function Name
ArgumentData Type of the RETURN VALUE
Example 2
Scope of Variables: General Sub Procedures and
Function Procedures
Change Trivial from a sub procedure to a function procedure as demonstrated below (the code in the black rectangles has been added)
Example 4
Passing Arguments ByVal and ByRef
• The arglist argument has the following syntax and parts:
• ([ByVal | ByRef] varname )
• Part– ByVal Indicates that the argument is passed by value
– ByRef Indicates that the argument is passed by reference
– Varname Name of the variable representing the argument
Passing Arguments ByVal and ByRef
• by value (ByVal) – A way of passing the value, rather than the address,
of an argument to a procedure– This allows the procedure to access a copy of the
variable– As a result, the variables actual value cannot be
changed by the procedure to which it is passed
Passing Arguments ByVal and ByRef
• by reference (ByRef) – A way of passing the address, rather than the value,
of an argument to a procedure– A pointer to the variable is passed to the procedure– This allows the procedure to access the actual
variable– As a result, the variables actual value can be
changed by the procedure to which it is passed
Passing Arguments ByVal and ByRef
• If you declare an argument without using either the ByVal or ByRef keyword, the argument will be passed by reference (ByRef) by default
Passing Arguments ByVal and ByRef
• A Single Argument in a SUB Procedure
• [1] GetNumbers (X)
• [2] GetNumbers X
• In situation [1] and [2] there is no need for the optional word CALL
• In situation [1] the argument X never changes– If you add the keyword CALL – it does change!
• In situation [2] the argument X changes to the new value
Passing Arguments ByVal and ByRef
• Multiple Argument
• [1] Call GetNumbers (X, Y)
• [2] GetNumbers X, Y
• In situation [1] CALL is required, otherwise it is interpreted as a function
• In situation [1] and [2] the arguments X and Y change to the new values
• Need to specify ByVal for unchanged values of X and Y
Note that when you use the Call keyword, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments
Passing Arguments ByVal and ByRef