General Sub Procedures / Passing Arguments to Procedures
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this movie we will discuss passing arguments, which may be done so by passing by value or by reference. At times you may need to use the value of a variable in one procedure and then again in the second procedure that is called from the first. In this situation you could declare the variable as module-level, but that approach might make the variable visible and will make the variable visible to all other procedures. To keep the scope of a variable as narrow as possible, consider declaring the variable as local and passing it to any called procedure. When a Sub Procedure defines an argument, any call to that procedure must supply that argument. In addition, the argument value must be the same data type in both locations. Another important point is that the name of the argument does not have to be the same in both locations, but the data type must agree. Because once the value has been passed to the receiving function or procedure, it is considered a local variable within that procedure. So it can have a different name. You can specify multiple arguments in both the Sub Procedure, header and the call to the procedure. However, keep in mind the number of arguments, their sequence and their data types must match at both locations. When you pass a value by procedure, you may pass it either by value or by reference. To pass an argument by value, you use the keyword By Val. To pass the value by reference, you use the keyword By Ref. You will see examples of both of these. When passing by value, therefore using the keyword By Val, what you are doing is you are sending a copy of the argument's value to the procedure. In this method the procedure can not change the original value. If you do not specify by value or by reference, the default is to be passed by value. The second alternative is to use the By Ref, which is passing by reference. When you do so, passing by reference will send a reference indicating the memory location of the value. This means that the procedure will be able to change the original value since it does know the memory location and has the ability to change it at the memory address. Remember, if you do not specify either by value or by reference, the default is by value. Here is an example of passing a value by value, a variable by value using a procedure called Calculate Pay. You'll notice the sub header is called Calculate Pay. Within the parentheses, instead of being empty as we've seen in the past, we are passing a variable called Hours Worked Decimal. You'll note this variable is of type decimal, but the key here is that we're passing this by value. This means that the receiving function will be able to perform any data on the Hours Worked Decimal, but it will not change the original value that was passed to it. However, when you pass the same data by reference, in this example we have the variable Hours Worked Decimal as decimal being passed by reference, we are now allowing the procedure to change the original value by changing it at its memory address. Remember, the default is to be passed by value. You want to take care when you pass data by reference since you're allowing the possibility of the original data to be changed. But oftentimes this is what you might want to have done. Keep in mind when you write a function that you declare the arguments that the function needs. That is, you give each argument an identifier and a data type. The name that you give an argument within the Function Procedure header is the identifier that you will use inside the function to refer to the value of that argument. In the Function Procedure, the argument list you enter will establish 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 use the predefined Visual Basic functions. The main difference between coding a Function Procedure and coding a Sub Procedure is that a Function Procedure must set up the return value. This return value is placed in a variable that Visual Basic names with the same name as the function name.
Tutorial Information
| Course: | Microsoft Visual Basic 2005 |
| Author: | Arthur Lee |
| SKU: | 33940 |
| ISBN: | 1-935320-10-6 |
| Release Date: | 2008-11-19 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 