Visual Basic I Programming CSC 162 Visual Basic I Programming
Arguments and Modules Sub Procedures vs. Functions Arguments (Parameters) ByVal and ByRef Usage Guidelines Optional Arguments Positional vs. Named Argument Association Modules
Sub Procedures vs. Functions Perform some “action”. Take in arguments (parameters). Can return value(s) by using ByRef argument(s). Functions: Perform some “calculation”. Return a single value through the function’s name.
ByVal and ByRef Usage Guidelines Use ByVal when: Parameter is only being read in to the function or sub procedure Use ByRef when: Parameter is only being sent out of the sub procedure Parameter is being read in to and sent out of the sub procedure
Optional Arguments Requirements: Example: Non-optional arguments must be listed before optional arguments. Each optional argument must have the reserved word Optional before it. Each optional argument must be given a default value. Example: Procedure declaration: Private Sub PrintArea(ByVal intX As Integer, _ Optional intY As Integer = 1) Call to procedure: Call PrintArea(5) ' intX=5, intY=1 Call PrintArea(7, 9) ' intX=7, intY=9
Positional vs. Named Argument Association Positional Argument Association Each argument is passed in to the procedure according to its position in the parentheses. Example: Procedure declaration: Private Sub PrintVolume(Optional intX As Integer = 1, _ Optional intY As Integer = 1, _ Optional intZ As Integer = 1) Call to procedure: Call PrintVolume(2, 3, 5) ' intX=2, intY=3, intZ=5 Call PrintVolume() ' intX=1, intY=1, intZ=1 Call PrintVolume(1, 1, 5) ' intX=1, intY=1, intZ=5 Call PrintVolume(, , 5) ' intX=1, intY=1, intZ=5 Call PrintVolume(4, , 5) ' intX=4, intY=1, intZ=5
Positional vs. Named Argument Association Each argument is passed in to the procedure according to its name in the procedure declaration. Example: Procedure declaration: Private Sub PrintVolume(Optional intX As Integer = 1, _ Optional intY As Integer = 1, _ Optional intZ As Integer = 1) Call to procedure: Call PrintVolume(intX:=2, intY:=3, intZ:=5) ' intX=2, intY=3, intZ=5 Call PrintVolume(intY:=3, intZ:=5, intX:=2) ' intX=2, intY=3, intZ=5 Call PrintVolume() ' intX=1, intY=1, intZ=1 Call PrintVolume(intZ:=5) ' intX=1, intY=1, intZ=5 Call PrintVolume(intX:=4, intZ:=5) ' intX=4, intY=1, intZ=5
Positional vs. Named Argument Association Example: Procedure declaration: Private Function Product(ByVal sngA As Single, _ ByVal sngB As Single) As Single Call to procedure: sngAnswer = Product(3, 5) ' sngA=3, sngB=5 sngAnswer = Product(sngA:=3, sngB:=5) ' sngA=3, sngB=5 sngAnswer = Product(sngB:=5, sngA:=3) ' sngA=3, sngB=5 sngAnswer = Product(5, 3) ' sngA=5, sngB=3
Modules A module is a file that contains only code. It is has no GUI associated with it. It is stored in a file with a .bas extension. Its code can be made available (Public) to other forms, modules, etc. in the same project. Since it is a stand-alone file, its code can be reused in other projects.
Programming Assignment 5 Lab Assignment Write a function DegToRad that converts an angle in degrees to radians. Use this function in a program that allows the user to input an angle (measured in degrees) in a textbox, and then computes the sine, cosine, tangent, cosecant, secant, and cotangent. Display these values in six textboxes with appropriate labels. Round all answers to four (4) decimal places Programming Assignment 5 Due Monday, November 10 / Tuesday, November 11 Page 241 #6.18 Page 241 #6.19