FUNCTIONS& PROCUDURES:
1. Procedures and functions provides a means of producing structured programs.
2. Rather than repeating the same operations at several different places in the program, they
can be placed in a procedure or function.
3. This not only makes the program easier to read, but saves a lot of time and effort in
maintaining the program.
PROCEDURES:
1. A procedure is a block of code that performs some operation.
2. A procedure is a block of Visual Basic statements inside Sub, End Sub statements.
3. Procedures do not return values.
basic Syntax for a procedure is:
[Private | Public][Static] Sub procName ([arglist])
Public
Indicates that the procedure is available to all modules. If Option Private is
used in the module, the procedure is not available to modules outside of
the project.
Private
Indicates that the procedure is only available to other procedures or
functions in the current module or form.
Static
Indicates that all variables declared within the procedure are retained, even
when the procedure is out of scope.
procName
The name of the procedure. Must be unique to the module if declared
Private, otherwise unique to the project. The name of the procedure
follows the naming rule for Variables.
arglist
A list of variables passed to the procedure as arguments, and their data
types. Multiple arguments are separated by commas. Arguments may be
Optional, and may be Read Only.
Visual Basic supports two ways of passing parameters to functions. By value and by
reference.
byval :
1. For this, we have two keywords. ByVal and ByRef. When we pass arguments by value,
the function works only with the copies of the values.
2. This may lead to performance overheads, when we work with large amounts of data.
3. ByVal indicates the variable was passed by value, and any changes made within the
procedure to the variable will not be reflected to where the procedure was called as it's
only a copy.
SAMPLE PROGRAM FOR BYVAL :
Private Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Add = x + y
End Function
Private Sub Form_Load()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = 32
b = 64
c = Add(a, b)
MsgBox c
End Sub
by ref :
1. When we pass values by reference, the function receives a reference to the actual values.
2. The original values are affected, when modified. This way of passing values is more time
and space efficient. On the other hand, it is more error prone.
3. ByRef indicates the variable is passed by reference, and any changes made within the
procedure to the variable will be reflected to where the procedure was called.
4. ByRef is the default in Visual Basic 6
SAMPLE PROGRAM FOR BYREF :
Private Sub Swap(ByRef x As Integer, ByRef y As Integer)
Dim temp As Integer
temp = x
x=y
y = temp
End Sub
Private Sub DisplayVals(ByRef a As Integer, ByVal b As Integer)
MsgBox "a = " & CStr(a) & vbCrLf & "b = " & CStr(b)
End Sub
Private Sub Form_Load()
Dim a As Integer
Dim b As Integer
a = 10
b = 12
'Display values, swap, and display again
DisplayVals a, b
Call Swap(a, b)
DisplayVals a, b
End Sub
2