Home

Arguments and Parameters

 

Arguments

 
 

Using Global Variables

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:

  • Private: A private global variable can be accessed by any procedure of the same file. No procedure of another file, even of the same program, can access it
  • Friend: A friendly global variable can be accessed by any procedure of any file of the same web site. A procedures of another web site cannot access that variable
  • Public: A public global variable can be accessed by any procedure of its web site and procedures of other projects

Based on this characteristic of the procedures of a web file having access to global variables of the same project, you can declare such variables and initialize or modify them in any procedure of the same code file.

Introduction to Arguments

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:

<script language="vbscript" type="text/vbsscript" runat="server">

    Sub ProcedureName(Argument)
      
    End Sub

</script>

If you are creating a function, the syntax would be:

<script language="vbscript" type="text/vbsscript" runat="server">

    Function ProcedureName(Argument) As DataType
      
    Function Sub

</script>

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:

<script language="vbscript" type="text/vbsscript" runat="server">

    Function CalculatePayroll(strName As String) As Double
      
    Function Sub

</script>

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:

<script language="vbscript" type="text/vbsscript" runat="server">

    Sub EvaluateInvoice(EmplName As String, HourlySalary As Currency)
      
    End Sub

</script>

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.

Calling a Procedure With Argument

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

    Private Function GetFullName$(strFirst As String, _
                                  strLast As String)
        Dim FName As String

        FName = strFirst & " " & strLast
        GetFullName = FName
    End Function

    Sub Welcome(ByVal strLanguage As String)
        Response.Write("Welcome to the wonderful world of " & strLanguage)
    End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim FirstName, LastName As String
    Dim FullName As String
    Dim ComputerLanguage As String = "Visual Basic"

    FirstName = "Christina"
    LastName = "Ramon"

    FullName = GetFullName(FirstName, LastName)
%>

<%
    Response.Write("Hello, " & FullName)
    Response.Write("<br />")
    Welcome(ComputerLanguage)
%>
</body>

This would produce:

Procedure

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:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

    Private Function GetFullName$(MI As String, _
				  LastName As String, _
				  FirstName As String)
        GetFullName = FirstName & " " & MI & " " & LastName
    End Function

    Private Sub Welcome(ByVal strLanguage As String)
        Response.Write("Welcome to the wonderful world of " & strLanguage)
    End Sub

</script>

<title>Exercise</title>

</head>
<body>

<%
    Dim FullName As String
    Dim ComputerLanguage As String = "VBasic"

    FullName = GetFullName(LastName:="Roberts", FirstName:="Alan", MI:="R.")
%>

<%
    Response.Write("Hello " & FullName)
    Response.Write("<br />")
    Call Welcome(ComputerLanguage)
%>
</body>

Techniques of Passing Arguments

 

Passing Arguments (By Value)

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:

<script language="vbscript" type="text/vbsscript" runat="server">

    Private Sub Welcome(ByVal strLanguage As String)
    	Response.Write("Welcome to the wonderful world of " & strLanguage)
    End Sub

</script>

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:

Passing Arguments By Reference

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.

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. 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.

 

Static Variables

 

Introduction

We know that, when a procedure is defined, any variable declared locally belongs to the procedure and its influence cannot 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.

 
 
 
 

Optional Arguments

 

Introduction

If you create a procedure that takes one or more arguments, whenever you call that procedure, you must provide a value for the argument(s). Otherwise,, you would receive an error. If such an argument is passed with the same value over and over again, you may be tempted to remove the argument altogether. In some cases, although a certain argument is passed with the same value most of the time, you still have situations in which you want the user to decide whether to pass a value or not for the argument, you can declare the value optional. In other words, you can create the argument with a default value so that the user can call the procedure without passing a value for the argument, thus passing a value only when necessary. Such an argument is called default or optional.

Imagine you write a procedure that will be used to calculate the final price of an item after discount. The procedure would need the discount rate in order to perform the calculation. Such a procedure could look like this:

<script language="vbscript" type="text/vbsscript" runat="server">

Function CalculateNetPrice#(ByVal DiscountRate As Double)
    Dim OrigPrice#

    OrigPrice = 125.55

    Return OrigPrice - (OrigPrice * DiscountRate / 100)
End Function

</script>

Since this procedure expects an argument, if you do not supply it, the following program would not compile:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

    Function CalculateNetPrice#(ByVal DiscountRate As Double)
    Dim OrigPrice#

    OrigPrice = 125.55

    Return OrigPrice - (OrigPrice * DiscountRate / 100)
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
Dim FinalPrice#
Dim Discount# = 15 ' That is 25% = 25

FinalPrice = CalculateNetPrice(Discount)
%>

<% Response.Write("Final Price = " & FinalPrice.ToString("C")) %>
</body>

This would produce:

Most of the time, a procedure such as ours would use the same discount rate over and over again. Therefore, instead of supplying an argument all the time, you can define an argument whose value would be used whenever the function is not provided with the argument.

Passing an Optional Argument

To specify that an argument is optional, when creating its procedure, type the Optional keyword to the left of the argument's name and assign it the default value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

    Function CalculateNetPrice#(Optional ByVal DiscountRate As Double = 20)
    Dim OrigPrice#

    OrigPrice = 125.55

    Return OrigPrice - (OrigPrice * DiscountRate / 100)
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
Dim FinalPrice#
Dim Discount# = 15 ' That is 25% = 25

FinalPrice = CalculateNetPrice()
%>

<% Response.Write("Final Price = " & FinalPrice.ToString("C")) %>
</body>

This would produce:

Optional

A Procedure With Many Optional Arguments

If 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 , provide the Optional keyword for each and assign it the desired default value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

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 = OrigPrice * Discount / 100
    Dim TaxValue As Double = Tax / 100
    Dim NetPrice As Double = OrigPrice - DiscountValue + TaxValue
    Dim Result As String

    Result = "Original Price: " & CStr(OrigPrice) & "<br />" & _
             "Discount Rate: " & CStr(Discount) & "%" & "<br />" & _
             "Tax Amount: " & CStr(Tax) & "<br />"
    Response.Write(Result)

    Return NetPrice
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
Dim FinalPrice As Double

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

</body>

This would produce:

Optional

If a procedure takes more than one argument as above, remember that some arguments can be specified as optional. In this case, when calling the procedure, any argument that does not have a default value must be passed with a value. When creating a procedure that takes more than one argument, the argument(s) that has(have) default value(s) must be the last in the procedure. This means that:

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

Because of this, when calling any procedure in the Visual Basic language, you must know what, if any, argument 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 that is (are) not optional. If you want to provide the value of one of the arguments but that argument is not the first optional, you can leave empty the position(s) of the other argument(s) but remember to type a comma to indicate that the position is that of an argument that has a default value. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" 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
        Dim Result As String

        Result = "Price Acquired: " & CStr(AcquiredPrice) & "<br />" & _
                 "Marked Price: " & vbTab & CStr(MarkedPrice) & "<br />" & _
                 "Discount Rate: " & vbTab & CStr(DiscountRate) & "%" & "<br />" & _
                 "Discount Amt: " & vbTab & CStr(DiscountAmount) & "<br />" & _
                 "Tax Rate: " & vbTab & CStr(TaxRate) & "%" & "<br />" & _
                 "Tax Amount: " & vbTab & CStr(TaxAmount) & "<br />"
        Response.Write(Result)

        Return NetPrice
End Function

</script>

<title>Exercise</title>

</head>
<body>

<%
Dim FinalPrice As Double

FinalPrice = CalculateNetPrice(225.55, 150.55, , 40)
%>

<% Response.Write("Final Price: " & CStr(FinalPrice)) %>

</body>

This would produce:

Optional

Procedure Overloading

 

Introduction

A program involves a great deal of names that represent variables and procedures of various kinds. The compiler does not allow two variables to have the same name in the same procedure (or in the same scope). Although two procedures should have unique names in the same program, you are allowed to use the same name for different procedures of the same program following certain rules.

Overloading a Procedure

The ability to have various procedures with the same name in the same program is referred to as overloading. The most important rule about procedure overloading is to make sure that each one of these procedures has a different number or different type(s) of arguments.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc. Home