Home

Arguments and Parameters

 
 

Arguments Fundamentals

 
 

Global Variables

 

We know 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:

  • Private: A private global variable can be accessed  by any procedure of the same module. No procedure of another module, even of the same program, can access it
     
    Global Variables
  • Friend: A friendly global variable can be accessed by any procedure of any module of the same project. A procedures of another program cannot access that variable
     
    Friend Access Modifier
  • Public: A public global variable can be accessed by any procedure of its project and procedures of other projects
     
    Access Levels

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.

Practical Learning: Using Global Variables

  1. To use global variables, change the document as follows:
     
    Public Module Rectangle
        Private Length As Double
        Private Width As Double
    
        Private Sub GetLength()
            Length = InputBox("Enter Rectangle Length:")
        End Sub
    
        Private Sub GetWidth()
            Width = InputBox("Enter Rectangle Width:")
        End Sub
    
        Private Function CalculatePerimeter() As Double
            CalculatePerimeter = (Length + Width) * 2
        End Function
    
        Public Function Main() As Integer
            Dim Perimeter As Double
            GetLength()
            GetWidth()
            Perimeter = CalculatePerimeter()
    
            MsgBox("=-= Square Characteristics=-=" & vbCrLf & _
                   "Length: " & vbTab & Length & vbCrLf & _
                   "Width: " & vbTab & Width & vbCrLf & _
                   "Perimeter: " & Perimeter)
    	Return 0
        End Function
    
    End Module
  2. Execute the application
  3. Enter the length as 32.08 and the with as 24.84
  4. Close the message box and the DOS window

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:

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.

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:

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

Practical Learning: Passing Arguments to a Procedure

  1. To pass arguments to a function, change the file as follows (when you type the argument, Microsoft Visual Studio, actually the Visual Basic language parser, will add the ByVal keywords; in the next sections, we will learn what that keyword means; for now, keep but ignore it):  
    Public Module Rectangle
    
        Private Function GetValue(TypeOfValue As String) As Double
            Dim Value As Double
    
            Value = InputBox("Enter the " & TypeOfValue & ":")
            Return Value
        End Function
    
        Private Function CalculatePerimeter(Length As Double, _
                                            Width As Double) As Double
            CalculatePerimeter = (Length + Width) * 2
        End Function
    
        Public Function Main() As Integer
            Dim L As Double, W As Double
            Dim Perimeter As Double
    
            L = GetValue("Length")
            W = GetValue("Width")
            Perimeter = CalculatePerimeter(L, W)
    
            MsgBox("=-= Square Characteristics=-=" & vbCrLf & _
                   "Length: " & L & vbCrLf & _
                   "Width: " & W & vbCrLf & _
                   "Perimeter: " & Perimeter)
    	Return 0
        End Function
    
    End Module
  2. Execute the application
  3. Enter the length as 44.14 and the with as 30.76
  4. Close the message box and the DOS window
 
 
 
 

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:

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:

Arguments

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

Practical Learning: Passing Arguments by Reference

  1. To pass an argument by reference, change the file as follows:
     
    Public Module Rectangle
    
        Private Sub GetValues(ByRef Length As Double, ByRef Width As Double)
            Length = InputBox("Enter the length:")
            Width = InputBox("Enter the width:")
        End Sub
    
        Private Function CalculatePerimeter(ByVal Length As Double, _
                                            ByVal Width As Double) As Double
            CalculatePerimeter = (Length + Width) * 2
        End Function
    
        Private Function CalculateArea(ByVal Length As Double, _
                                            ByVal Width As Double) As Double
            CalculateArea = Length * Width
        End Function
    
        Private Sub ShowCharacteristics(ByVal Length As Double, _
                                            ByVal Width As Double)
            Dim Result As String
    
            Result = "=-= Rectangle Characteristics =-=" & vbCrLf & _
                           "Length: " & vbTab & vbTab & CStr(Length) & vbCrLf & _
                           "Width: " & vbTab & vbTab & CStr(Width) & vbCrLf & _
                           "Perimeter: " & vbTab & _
                           CalculatePerimeter(Length, Width) & vbCrLf & _
                           "Area: " & vbTab & vbTab & _
    			CalculateArea(Length, Width)
            MsgBox(Result)
        End Sub
    
        Public Function Main() As Integer
            Dim L As Double, W As Double
    
            GetValues(L, W)
            ShowCharacteristics(L, W)
    	Return 0
        End Function
    
    End Module
  2. Execute the application
  3. Enter the length as 24.55 and the width as 22.85
     
  4. Close the message box and the DOS window
 
 
   
 

Previous Copyright © 2009-2010 FunctionX, Inc. Next