![]() |
Introduction to Procedures and Functions |
|
Introduction to Procedures |
|
Procedures |
|
A procedure is a set-aside assignment the compiler must take care of to complement a program. A program developer writes the procedure and a user (the user is either you or another programmer) would only see the result. There are two categories of procedures you will use in your programs: those that have already been created thus made available to you, and those you will create yourself. |
|
In the Visual Basic language, like most other languages, there are two types of procedures: functions and sub routines. A sub procedure is an assignment that is carried but does not give back a result. To create a sub procedure, start by typing the Sub keyword followed by a name (like everything else, a procedure must have a name). The name of a procedure is always followed by parentheses. At the end of the sub procedure, you must type End Sub. Therefore, the primary syntax of a sub procedure is: |
In the previous lesson, we saw that you could declare a global variable outside of any procedure. When using various procedures in a code file, one of the characteristics of a global variable is that it is automatically accessible to other procedures:
Based on this characteristic of the procedures of a module having access to global variables of the same program, you can declare such variables and initialize or modify them in any procedure of the same code file.
So far, to use a value in a procedure, we had to declare it. In some cases, a procedure may need an external value in order to carry its assignment. A value that is supplied to a procedure is called an argument. When creating a procedure that will use an external value, declare the argument that represents that value between the parentheses of the procedure. For a sub procedure, the syntax you use would be: Sub ProcedureName(Argument)
End Sub
If you are creating a function, the syntax would be: Function ProcedureName(Argument) As DataType
Function Sub
The argument must be declared as a normal variable, omitting the Dim keyword. Here is an example that creates a function that takes a string as argument: Function CalculatePayroll(strName As String) As Double
Function Sub
A certain procedure can take more than one argument. In this case, in the parentheses of the procedure, separate the arguments with a comma. Here is an example of a sub procedure that takes two arguments: Sub EvaluateInvoice(EmplName As String, HourlySalary As Currency)
End Sub
In the body of a procedure that takes one or more arguments, use the argument(s) as you see fit as if they were locally declared variables. For example, you can involve them with values inside of the procedure. You can also exclusively use the values of the arguments to perform the assignment.
To call a procedure that takes an argument, type its name and a space, followed by a value for each argument between parentheses. The value provided for an argument is also called a parameter. If there is more than one argument, separate them with a comma. Here is an example: Module Exercise
Private Function GetFullName$(strFirst As String, _
strLast As String)
Dim FName As String
FName = strFirst & " " & strLast
GetFullName = FName
End Function
Public Function Main() As Integer
Dim FirstName, LastName As String
Dim FullName As String
Dim ComputerLanguage As String = "Visual Basic"
FirstName = inputbox("Enter First Name: ")
LastName = inputbox("Enter Last Name: ")
FullName = GetFullName(FirstName, LastName)
msgbox("Hello, " & FullName)
Welcome(ComputerLanguage)
Return 0
End Function
Sub Welcome(ByVal strLanguage As String)
msgbox("Welcome to the wonderful world of " & strLanguage)
End Sub
End Module
As mentioned previously, you can also use the Call keyword to call a procedure. When you call a procedure that takes more than one argument, you must provide the values of the arguments in the exact order they are listed inside of the parentheses of the function. Fortunately, you do not have to. If you know the names of the arguments, you can type them in any order and provide a value for each. To do that, on the right side of each argument, type the := operator followed by the desired value for the argument. Here is an example: Public Module Exercise
Private Function GetFullName$(MI As String, _
LastName As String, _
FirstName As String)
GetFullName = FirstName & " " & MI & " " & LastName
End Function
Public Function Main() As Integer
Dim FullName As String
Dim ComputerLanguage As String = "VBasic"
FullName = GetFullName(LastName:="Roberts", FirstName:="Alan", MI:="R.")
MsgBox("Hello " & FullName)
Call Welcome(ComputerLanguage)
Return 0
End Function
Private Sub Welcome(ByVal strLanguage As String)
MsgBox("Welcome to the wonderful world of " & strLanguage)
End Sub
End Module
When calling a procedure that takes an argument, we were supplying a value for that argument. When this is done, the procedure that is called makes a copy of the value of the argument and makes that copy available to the calling procedure. That way, the argument itself is not accessed. This is referred to as passing an argument by value. This can be reinforced by typing the ByVal keyword on the left side of the argument. Here is an example: Private Sub Welcome(ByVal strLanguage As String)
MsgBox("Welcome to the wonderful world of " & strLanguage)
End Sub
If you create a procedure that takes an argument by value and you have used the ByVal keyword on the argument, when calling the procedure, you do not need to use the ByVal keyword; just the name of the argument is enough, as done in the examples on arguments so far. Here is an example: Public Module Exercise
Public Function Main() As Integer
Dim ComputerLanguage As String = "Visual Basic"
Welcome(ComputerLanguage)
Return 0
End Function
Private Sub Welcome(ByVal strLanguage As String)
MsgBox("Welcome to the wonderful world of " & strLanguage)
End Sub
End Module
This would produce:
An alternative to passing arguments as done so far is to pass the address of the argument to the called procedure. When this is done, the called procedure does not receive a simple copy of the value of the argument: the argument is accessed by its address. That is, at its memory address. With this technique, any action carried on the argument will be kept. If the value of the argument is modified, the argument would now have the new value, dismissing or losing the original value it had. This technique is referred to as passing an argument by reference. Consider the following program: Public Module Exercise
Private Function Addition#(ByVal Value1 As Double, ByVal Value2 As Double)
Value1 = InputBox("Enter First Number: ")
Value2 = InputBox("Enter Second Number: ")
Addition = Value1 + Value2
End Function
Public Function Main() As Integer
Dim Result As String
Dim Number1, Number2 As Double
Result = Addition(Number1, Number2)
MsgBox(Number1 & " + " & Number2 & " = " & Result)
Return 0
End Function
End Module
Here is an example of running the program:
Notice that, although the values of the arguments were changed in the Addition() procedure, at the end of the procedure, they lose the value they got in the function. If you want a procedure to change the value of an argument, you can pass the argument by reference. To pass an argument by reference, on its left, type the ByRef keyword. This is done only when creating the procedure. When the called procedure finishes with the argument, the argument would keep whatever modification was made on its value. Now consider the same program as above but with arguments passed by reference: Public Module Exercise
Private Function Addition#(ByRef Value1 As Double, ByRef Value2 As Double)
Value1 = InputBox("Enter First Number: ")
Value2 = InputBox("Enter Second Number: ")
Addition = Value1 + Value2
End Function
Public Function Main() As Integer
Dim Result As String
Dim Number1, Number2 As Double
Result = Addition(Number1, Number2)
MsgBox(Number1 & " + " & Number2 & " = " & Result)
Return 0
End Function
End Module
Here is an example of running the program:
Using this technique, you can pass as many arguments by reference and as many arguments by value as you want. As you may guess already, this technique is also used to make a sub procedure return a value, which a regular sub routine cannot do. Furthermore, passing arguments by reference allows a procedure to return as many values as possible while a regular function can return only one value.
|
|
|
||
| Previous | Copyright © 2008-2010 FunctionX | Next |
|
|
||