Home

The Parameters of a Procedure

A Function or Procedure With a Parameter

Introduction

A procedure or a function may need an external value in order to perform its action. A value that is supplied to a procedure is called a parameter.

When creating a procedure or function that will use an external value, in the parentheses of the procedure or function, write the name of the parameter followed by As and the data type of the parameter. Based on this, the formula to follow for a procedure is:

Sub procedure-name(parameter-name As data-type)
      
End Sub

The formula to follow for a function is:

Function function-name(parameter-name As data-type) As DataType
      
Function Sub

Here is an example of procedure with a string-based parameter and a function with a numeric parameter:

<script runat="server">
Sub Identify(Name As String)

End Sub

Function CalculatePerimeter(Side As Double) As Double
    Return 0.00
End Function
</script>

In the body of the procedure or function, you can ignore the parameter by not using it at all. Otherwise, use the parameter as you would use a local or a global variable. Here are examples:

<script runat="server">
Sub Identify(Name As String)
    Response.Write("<h3>Geometry: " & Name & "</h3>")
End Sub

Function CalculatePerimeter(Side As Double) As Double
    Dim Perimeter As Double

    Perimeter = Side * 4.00

    Return Perimeter
End Function
</script>

An Argument

To call a procedure or a function that take a parameter, in the parentheses of the function or procedure, entehr the desired value. The value provided is called an argument. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Sub Identify(Name As String)
    Response.Write("<h3>Geometry: " & Name & "</h3>")
End Sub

Function CalculatePerimeter(Side As Double) As Double
    Dim Perimeter As Double

    Perimeter = Side * 4.00

    Return Perimeter
End Function
</script>
<title>Geometric Figures: The Square</title>

</head>
<body>
<%
Dim p As Double
 
p = CalculatePerimeter(138.74)

Identify("Square")

Response.Write("Side: 138.74")
Response.Write("<br>Perimeter: " & p)
%>

</body>
</html>

This would produce:

An Argument

You can also use a variable that holds the desired value and type that variable in the parentheses of the procedure or function. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script Language="VB" runat="server">
Sub Identify(Name As String)
    Response.Write("<h3>Geometry: " & Name & "</h3>")
End Sub

Function CalculatePerimeter(Side As Double) As Double
    Dim Perimeter As Double

    Perimeter = Side * 4.00

    Return Perimeter
End Function
</script>
<title>Geometric Figures: The Square</title>

</head>
<body>
<%
Dim n As String
Dim s As Double
Dim p As Double

s = 138.74
n = "Square"
p = CalculatePerimeter(s)

Identify(n)

Response.Write("Side: " & s)
Response.Write("<br>Perimeter: " & p)
%>

</body>
</html>

As mentioned previously, you can also use the Call keyword to call a procedure.

A Function or Procedure With Many Parameters

Introduction

A function or a procedure can use more than one parameter. In this case, the parameters are separated by commas. In the body the procedure or functionm, you can ignore all parameters, you can decide to use only one or some of them, or you can use all parameters. Here is an example:

<script runat="server">
Sub Identify(Number As Long, FirstName As String,
             LastName As String, MiddleInitial As Char)
    Response.Write(CStr(Number) & ": " & FirstName & " " & MiddleInitial & ". " & LastName)
End Sub

Function CalculateNetPay(HourlyRate As Double, TotalTime As Double) As Double
    Dim TotalSalary As Double

    TotalSalary = HourlyRate * TotalTime

    CalculateNetPay = TotalSalary
End Function
</script>

Calling a Function or Procedure With Many Arguments

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. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Sub Identify(Number As Long, FirstName As String,
             LastName As String, MiddleInitial As Char)
    Response.Write(Number & ": " & FirstName & " " &
                   MiddleInitial & ". " & LastName )
End Sub

Function CalculateNetPay(HourlyRate As Double, TotalTime As Double) As Double
    Dim TotalSalary As Double

    TotalSalary = HourlyRate * TotalTime

    CalculateNetPay = TotalSalary
End Function
</script>

<title>Employee Payroll</title>
</head>
<body>
<h3>Employee Payroll</h3>

<%
Dim EmployeeNumber = 502848
Dim HourlySalary = 18.85
Dim TimeWorked = 44.00
Dim First = "David", MI = "K"c, Last = "Golden"
Dim Pay = CalculateNetPay(HourlySalary, TimeWorked)

Response.Write("<b>Employee:</b> ")
Identify(EmployeeNumber, First, Last, MI)
Response.Write("<br><b>Hourly Salary:</b> " & HourlySalary)
Response.Write("<br><b>Time Worked:</b> " & TimeWorked)
Response.Write("<br><b>Net Pay:</b> " & Pay)
%>

</body>
</html>

This would produce:

Calling a Function or Procedure With Many Arguments

Accessing an Argument by Name

When calling a procedure or a function, instead of providing the arguments in the exact order the parameters appear, you can access each parameter by its name and assign the desired value. To do this, in the parentheses of the procedure or function, type the name of parameter, followed by the := operator, followed by the desired value for the argument. You can list the combinations in any order of your choice and the assignments must be separated by commas. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Sub Identify(Number As Long, FirstName As String,
             LastName As String, MiddleInitial As Char)
    Response.Write(Number & ": " & FirstName & " " &
                   MiddleInitial & ". " & LastName )
End Sub

Function CalculateNetPay(HourlyRate As Double, TotalTime As Double) As Double
    Dim TotalSalary As Double

    TotalSalary = HourlyRate * TotalTime

    CalculateNetPay = TotalSalary
End Function
</script>

<title>Employee Payroll</title>
</head>
<body>
<h3>Employee Payroll</h3>

<%
Dim HourlySalary = 24.05
Dim TimeWorked = 42.5
Dim Pay = CalculateNetPay(TotalTime := TimeWorked, HourlyRate := HourlySalary)

Response.Write("<b>Employee:</b> ")
Identify(FirstName := "Justine", LastName := "Gibbs",
         Number := 381751, MiddleInitial :=  "V"c)
Response.Write("<br><b>Hourly Salary:</b> " & HourlySalary)
Response.Write("<br><b>Time Worked:</b> " & TimeWorked)
Response.Write("<br><b>Net Pay:</b> " & Pay)
%>

</body>
</html>

This would produce:

Accessing an Argument by Name

Techniques of Passing Arguments

Passing an Argument By Value

When calling a procedure or function that takes an argument, the procedure or function makes a copy of the value of the argument and makes that copy available to the calling procedure or function. The parameter itself is not accessed. This is referred to as passing an argument by value. To indicate this concept, type the ByVal keyword on the left side of the argument. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Sub Identify(ByVal Number As Long, ByVal FirstName As String,
             ByVal LastName As String, ByVal MiddleInitial As Char)
    Response.Write(CStr(Number) & ": " & FirstName & " " &
                        MiddleInitial & ". " & LastName )
End Sub

Function CalculateNetPay(ByVal HourlyRate As Double,
                         ByVal TotalTime As Double) As Double
    Return HourlyRate * TotalTime
End Function
</script>

<title>Employee Payroll</title>
</head>
<body>
<h3>Employee Payroll</h3>

<%
Dim HourlySalary = 24.05
Dim TimeWorked = 42.5
Dim Pay = CalculateNetPay(TotalTime := TimeWorked, HourlyRate := HourlySalary)

Response.Write("<b>Employee:</b> ")
Identify(FirstName := "Justine", LastName := "Gibbs",
         Number := 381751, MiddleInitial :=  "V"c)
Response.Write("<br><b>Hourly Salary:</b> " & HourlySalary)
Response.Write("<br><b>Time Worked:</b> " & TimeWorked)
Response.Write("<br><b>Net Pay:</b> " & Pay)
%>

</body>
</html>

This would produce the same result as above.

Passing an Argument By Reference

When passing an argument to a function, instead of passing the value of the argument, you can first declare a variable that would hold the value but instead pass the address of the variable. This technique is referred to as passing an argument by reference. When this is done, any action performed on the argument will be kept. For example, if the value of the argument is modified, the argument would now have the new value, dismissing or losing the original value it had.

To pass an argument by reference, in the parentheses of the procedure or function, instead of applying ByVal to the parameter, type the ByRef keyword. When calling the procedure or function, you must pass the name of an initialized variable for the parameter. Here is an example:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script Language="VB" runat="server">
Sub PassByReference(ByVal Number As Integer, ByRef Value As Integer)
    Response.Write("<h5>Inside the procedure - Start</h5>")
    Response.Write("Number = " & Number)
    Response.Write("<br><span style='color: red'>Value = " & Value & "</span>")

    Number = 800
    Value  = 911

    Response.Write("<h5>Inside the procedure - End</h5>")
    Response.Write("Number = " & Number)
    Response.Write("<br><span style='color: red'>Value = " & Value & "</span>")
End Sub

</script>

<title>Exercise</title>
</head>
<body>

<%
Dim n = 2405
Dim v = 1370

Response.Write("<h5>Initial Values:</h5> ")
Response.Write("Number = " & n)
Response.Write("<br><span style='color: blue'>Value = " & v & "</span>")

Response.Write("<h6>Calling the procedure</h6>")

PassByReference(n, v)

Response.Write("<h6>After calling the procedure</h6>")

Response.Write("<h5>Final Values:</h5> ")
Response.Write("Number = " & n)
Response.Write("<br><span style='color: blue'>Value = " & v & "</span>")
%>

</body>
</html>

This would produce:

Passing an Argument By Reference

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 can be used to make a procedure return one or more values, which a regular procedure cannot do. Furthermore, passing arguments by reference allows a function or a procedure to return as many values as possible while a regular function can return only one value.

Other Techniques of Passing Arguments

Optional Arguments

If you create a procedure or a function that takes one or more arguments, whenever you call that procedure, you must provide a value for each parameter. If a certain argument is passed with the same value almost ever time, you can provide a default value to the parameter. Its argument becomes referred to as optional.

To specify that an argument is optional, when creating its procedure or function, in the parentheses, type the Optional keyword to the left of the ByVal qualifier of the parameter and assign the desired default value to it. Here is an example:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script Language="VB" runat="server">
Dim OriginalPrice# = 199.95

Function CalculateNetPrice#(Optional ByVal DiscountRate As Double = 20.00)
    Return OriginalPrice - (OriginalPrice * DiscountRate / 100.00)
End Function
</script>

<title>Department Store</title>
</head>
<body>
<h4>Department Store</h4>

<%
' Discount Rate = 35%
Dim Rate = 35.00

Dim FinalPrice# = 0.00

FinalPrice# = CalculateNetPrice()
Response.Write("Original Price = " & OriginalPrice)
Response.Write("<br>Price After Default 20% Discount = " & FinalPrice)

Response.Write("<p>-------------------------</p>")

FinalPrice# = CalculateNetPrice(Rate)
Response.Write("Original Price = " & OriginalPrice)
Response.Write("<br>Price After 35% Discount = " & FinalPrice)
%>

</body>
</html>

This would produce:

Optional Arguments

If a function or a procedure takes more than one argument, you can provide a default argument for each and select which ones would have default values. If you want all arguments to have default values, when defining the procedure or function, provide the Optional keyword for each parameter and assign the desired default value to it. Here is an example:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script Language="VB" runat="server">
Dim OriginalPrice# = 199.95

Function CalculateNetPrice#(Optional ByVal Tax As Double = 5.75,
                            Optional ByVal Discount As Double = 25,
                            Optional ByVal OrigPrice As Double = 245.55)
    Dim DiscountValue As Double = OriginalPrice * Discount / 100
    Dim TaxValue As Double = Tax / 100
    Dim NetPrice As Double = OriginalPrice - DiscountValue + TaxValue
       
    Response.Write("Original Price: " & OriginalPrice)
    Response.Write("<br>Discount Rate: " & Discount & "%")
    Response.Write("<br>Tax Amount: " & Tax)

    Return NetPrice
End Function
</script>

<title>Department Store</title>
</head>
<body>
<h4>Department Store</h4>

<%
Dim FinalPrice As Double

FinalPrice = CalculateNetPrice()
Response.Write("<br>Final Price: " & FinalPrice)
%>

</body>
</html>

This would produce:

Optional Arguments

If you are creating a procedure or a function that has more than one parameter and a (some) parameter(s) must be optional, the parameter(s) that has(have) (a) default value(s) must be the last in the procedure. This means that:

  • If a procedure or a function has two parameters and one parameter has a default value, this optional parameter must be the second in the list of parameters
  • If a procedure or function is taking three or more parameters and two or more parameters have default values, these optional parameters must by placed to the right of the non-optional parameter(s).

Because of this, when calling any procedure in the Visual Basic language, you must know what, if any, parameter is optional and which one is not.

If a procedure takes two arguments and one argument has a default value, when calling this procedure, you can pass only one value. In this case, the passed value would be applied on the first argument. If a procedure takes more than two arguments and two or more arguments have a default value, when calling this procedure, you can provide only the value(s) of the argument(s) that is (are) not optional. A better alternative is to access the parameters by name to identify only the parameters that need values. Here is an example:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script Language="VB" runat="server">

Function CalculateNetPrice(ByVal AcquiredPrice As Double,
                           ByVal MarkedPrice As Double,
                           Optional ByVal TaxRate As Double = 5.75,
                           Optional ByVal DiscountRate As Double = 25) As Double
    Dim DiscountAmount As Double = MarkedPrice * DiscountRate / 100
    Dim TaxAmount As Double = MarkedPrice * TaxRate / 100
    Dim NetPrice As Double = MarkedPrice - DiscountAmount + TaxAmount

    Response.Write("Price Acquired: " & AcquiredPrice)
    Response.Write("<br>Marked Price: " & MarkedPrice)
    Response.Write("<br>Discount Rate: " & DiscountRate & "%")
    Response.Write("<br>Discount Amt: " & DiscountAmount)
    Response.Write("<br>Tax Rate: " & TaxRate & "%")
    Response.Write("<br>Tax Amount: " & TaxAmount)
    
    Return NetPrice
End Function
</script>

<title>Department Store</title>
</head>
<body>
<h4>Department Store</h4>

<%
Dim FinalPrice As Double

FinalPrice = CalculateNetPrice(MarkedPrice := 150.55,
                               DiscountRate := 40.00,
                               AcquiredPrice := 225.55)
Response.Write("<br>Final Price: " & FinalPrice)
%>

</body>
</html>

This would produce:

Optional Arguments

Overloading a Function or a Procedure

You can create many procedures or functions that have the same name in the same scope. The ability to have various procedures with the same name is referred to as overloading. When creating the procedures or functions, each one of them must have a different number of, and (a) different type(s) of, parameters. When calling a version of the procedures, make sure you pass the appropriate type(s) or (number) of argument(s) to indicate the one you want. Here is an example:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Function CalculateArea(ByVal side As Double) As Double
        Return side * side
End Function

Function CalculateArea(ByVal width As Double, ByVal height As Double) As Double
        Return width * height
End FunctionOverloads
</script>

<title>Geometric Figures</title>

</head>
<body>
<%
Dim length As Double
Dim height As Double

length = 118.27
height = 74.93

Dim square = CalculateArea(length)
Dim rectangle = CalculateArea(length, height)

Response.Write("Square")
Response.Write("<br>Side: " & length)
Response.Write("<br>Area: " & square)

Response.Write("<br><br>Rectangle")
Response.Write("<br>Length: " & length)
Response.Write("<br>Height: " & height)
Response.Write("<br>Area: " & rectangle)
%>

</body>
</html>

To re-enforce the idea that the function or procedure is overloaded, type the Overloads keywork to the left each of the Function keywords. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head>
<script runat="server">
Overloads Function CalculateArea(ByVal side As Double) As Double
        Return side * side
End Function

Overloads Function CalculateArea(ByVal width As Double, ByVal height As Double) As Double
        Return width * height
End Function
</script>
<title>Geometric Figures</title>

</head>
<body>
<%
Dim length As Double
Dim height As Double

length = 118.27
height = 74.93

Dim square = CalculateArea(length)
Dim rectangle = CalculateArea(length, height)

Response.Write("Square")
Response.Write("<br>Side: " & length)
Response.Write("<br>Area: " & square)

Response.Write("<br><br>Rectangle")
Response.Write("<br>Length: " & length)
Response.Write("<br>Height: " & height)
Response.Write("<br>Area: " & rectangle)
%>

</body>
</html>

Read-Only Variables

We already know that a variable is constant if its value never changes. Such a variable can be declared in a code delimiter or in a procedure/function. A read-only variable is a constant type that be declared only as a global variable. It is created using the ReadOnly keyword.

As done for a constant, when declaring a ReadOnly variable, you should initialize it. If you don't, it would receive a default value based on its type. For example, a number-based variable would be initialized with 0 and a String variable would be initialized with an empty string.

Static Variables

Introduction

Consider the following code:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head runat="server">
<script runat="server">
Private Sub Starter(ByVal y As Integer)
    Dim a As Double = 112.5
    Dim b As Double = 175.25

    a = a / y
    b = b + 2

    Response.Write("y   = " & CStr(y) &
                   "<br>a   = " & CStr(a) &
                   "<br>b   = " & CStr(b) &
                   "<br>b/a = " & CStr(b / a))
End Sub
</script>
<title>Exercise</title>
</head>
<body>
<%
Starter(2)
Response.Write("<br><br>")
Starter(2)
Response.Write("<br><br>")
Starter(2)
Response.Write("<br><br>")
Starter(2)
%>
</body>
</html>

This would produce:

Static Variables

A variable declared locally in a procedure belongs to it and its influence doesn't expand beyond the body of the procedure. If you want a locally declared variable to keep its changed value when its host procedure is exited, declare such a variable as static.

Declaring a Static Variable

To declare a static variable, type the Static keyword on the left of the Dim keyword. You should always initialize a static variable before using it. Here are examples:

<%@ Page Language="VB" %>

<!DOCTYPE html>

<html>
<head runat="server">
<script runat="server">
Private Sub Starter(ByVal y As Integer)
    Static Dim a As Double = 112.5
    Static Dim b As Double = 175.25

    a = a / y
    b = b + 2

    Response.Write("y   = " & CStr(y) &
                   "<br>a   = " & CStr(a) &
                   "<br>b   = " & CStr(b) &
                   "<br>b/a = " & CStr(b / a))
End Sub
</script>
<title>Exercise</title>
</head>
<body>
<%
Starter(2)
Response.Write("<br><br>")
Starter(2)
Response.Write("<br><br>")
Starter(2)
Response.Write("<br><br>")
Starter(2)
%>
</body>
</html>

This time, when executing the program, it would produce:

Static Variables

Notice that, this time, each local variable keeps its newly changed value when the function exits.