How to Pass Argument Functions in Visual Basic
The Visual Basic .NET programming environment is an excellent learning tool if you want to try your hand at computer programming. Microsoft offers the Express version as a free download, making it easy to obtain and get started. As with all object-oriented programming languages, Visual Basic facilitates the use of procedures in the form of sub procedures and function procedures. Both accept arguments, or parameters, that the calling statement passes to them. Passing arguments to a procedure is a way to make the procedure more flexible while practicing the object-oriented programming concept of reusable code.
Instructions
-
-
1
Determine the number of arguments you want your procedure to accept and code the first statement of the procedure as follows:
<access> Sub Procedure <NameOfSubProcedure> (<byvalbyref> <argument> as <datatype>)
"Access" indicates the scope of use for this procedure and can be "Private," "Public," "Friend" or "Protected." "Name of Procedure" is the name of your sub procedure, which should be indicative of its functionality. "Byvalorbyref" is either "ByVal" or "ByRef" as explained in the next step. "Argument" is the argument you will pass to the sub procedure, and "datatype" is the data type of the "argument."
-
2
Determine whether you want to pass the parameter by value (ByVal) or by reference (ByRef) and indicate this in the first statement. "By value" means that the procedure makes a copy of the original variable being passed and does not touch that original data field. "By reference" means that the procedure does not make a copy, but uses the actual data field passed to it, and can make changes to it. Although there may be circumstances where you will want the procedure to change the value of a parameter, it is typically not a good idea to allow your procedure to change the original data field, so "ByVal" is the Visual Basic default.
-
-
3
Code the first statement of your procedure with a list of arguments if you wish to pass more than one argument to it. Such a statement would look something like this function procedure example:
Private Function Calc (ByVal Qty as Integer, ByVal Disc as Decimal, ByVal Base as Decimal) as Decimal
Calc = Qty * ((1-Disc) * Base)
End Function
This procedure accepts three arguments, calculates the price and returns the number to the calling statement, which would look something like this:
Price = Calc(Qty, Disc, Base)
It is important that the calling statement provide the arguments as the exact type and number that the procedure is expecting.
-
1