Home

Arguments and Parameters

 

Introduction

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 routine, 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 only 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 routine 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.

Practical Learning: Passing Arguments to a Procedure

  1. To pass arguments to a function, change the file as follows:
     
    
    Module Module1
    
        Sub SquarePerimeter()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            Console.Write("Enter Side: ")
            dblSide = Console.ReadLine()
    
            dblPerimeter = dblSide * 4
    
            Console.WriteLine("Square Characteristics")
            Console.WriteLine("Side:      {0:F}", dblSide)
            Console.WriteLine("Perimeter: {0:F}", dblPerimeter)
        End Sub
    
        Function RectPerimeter(dblLen As Double, dblWid As Double)
            RectPerimeter = (dblLen + dblWid) * 2
        End Function
    
        Function RectArea(dblLen As Double, dblWid As Double)
            RectArea = dblLen * dblWid
        End Function
    
        Function Main() As Integer
    	Return 0
        End Function
    
    End Module
  2. Save the file
 
 

Passing Arguments (By Value)

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 Module1

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

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

    Sub Main()
        Dim FirstName, LastName As String
        Dim FullName As String
        Dim ComputerLanguage As String = "VBasic"

        Console.Write("Enter First Name: ")
        FirstName = Console.ReadLine()
        Console.Write("Enter Last Name: ")
        LastName = Console.ReadLine()

        FullName = GetFullName(FirstName, LastName)

        Console.Write("Hello {0}, ", FullName)
        Welcome(ComputerLanguage)

        Console.WriteLine()
    End Sub

    Sub Welcome(strLanguage As String)
        Console.WriteLine("Welcome to the wonderful world of {0}", strLanguage)
    End Sub

End Module

Here is an example of running the program:

Enter First Name: Ernestine
Enter Last Name: Fuller
Hello Ernestine Fuller, Welcome to the wonderful world of VBasic

As mentioned previously, you can also use the Call keyword to call a procedure. Here is an example:


Module Module1

    Sub Main()
        Call Welcome(ComputerLanguage)

        Console.WriteLine()
    End Sub

    Sub Welcome(ByVal strLanguage As String)
        Console.WriteLine("Welcome to the wonderful world of {0}", strLanguage)
    End Sub

End Module

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 don't 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:


Module Module1

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

    Sub Main()
        Dim FullName As String
        Dim ComputerLanguage As String = "VBasic"

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

        Console.Write("Hello {0}, ", FullName)
        Call Welcome(ComputerLanguage)

        Console.WriteLine()
    End Sub

    Sub Welcome(strLanguage As String)
        Console.WriteLine("Welcome to the wonderful world of {0}", strLanguage)
    End Sub

End Module

This would produce:

Hello Alan R. Roberts, Welcome to the wonderful world of VBasic

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:


Module Module1
    Sub Welcome(ByVal strLanguage As String)
        Console.WriteLine("Welcome to the wonderful world of {0}", strLanguage)
    End Sub
End Module

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 don't 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:


Module Module1

    Sub Main()
        Dim ComputerLanguage As String = "VBasic"

        Welcome(ComputerLanguage)

        Console.WriteLine()
    End Sub

    Sub Welcome(ByVal strLanguage As String)
        Console.WriteLine("Welcome to the wonderful world of {0}", strLanguage)
    End Sub

End Module

This would produce:

Welcome to the wonderful world of VBasic
 

Practical Learning: Passing Arguments by Value

  1. To pass arguments by value, change the file as follows:
     
    
    Module Module1
    
        Sub SquarePerimeter()
            Dim dblSide As Double
            Dim dblPerimeter As Double
    
            Console.Write("Enter Side: ")
            dblSide = Console.ReadLine()
    
            dblPerimeter = dblSide * 4
    
            Console.WriteLine("Square Characteristics")
            Console.WriteLine("Side:      {0:F}", dblSide)
            Console.WriteLine("Perimeter: {0:F}", dblPerimeter)
        End Sub
    
        Function RectPerimeter(ByVal dblLen As Double, ByVal dblWid As Double)
            RectPerimeter = (dblLen + dblWid) * 2
        End Function
    
        Function RectArea(ByVal dblLen As Double, ByVal dblWid As Double)
            RectArea = dblLen * dblWid
        End Function
    
    
        Function Main() As Integer
            Dim dblLength, dblWidth As Double
            Dim Perimeter, Area As Double
    
            Console.Write("Enter Rectangle Length: ")
            dblLength = Console.ReadLine()
            Console.Write("Enter Rectangle Width:  ")
            dblWidth = Console.ReadLine()
    
            Perimeter = RectPerimeter(dblLength, dblWidth)
            Area = RectArea(dblLength, dblWidth)
    
            Console.WriteLine("Rectangle Characteristics")
            Console.WriteLine("Length:    {0:F}", dblLength)
            Console.WriteLine("Width:     {0:F}", dblWidth)
            Console.WriteLine("Perimeter: {0:F}", Perimeter)
            Console.WriteLine("Area:      {0:F}", Area)
    
            Console.WriteLine()
    	Return 0
        End Function
    
    End Module
  2. Save the file
  3. Compile the application with vbc Exercise.vb
  4. Execute the application with Exercise. Here is an example of running the application
     
    Enter Rectangle Length: 35.88
    Enter Rectangle Width:  32.48
    
    Rectangle Characteristics
    Length:    35.88
    Width:     32.48
    Perimeter: 136.72
    Area:      1165.38
  5. Close the form and return to MSVB
 

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 doesn't receive a simple copy of the value of the argument: the argument is accessed at its root. 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:


Module Module1

    Function Addition#(ByVal Value1 As Double, ByVal Value2 As Double)
        Console.Write("Enter First Number:  ")
        Value1 = Console.ReadLine()
        Console.Write("Enter Second Number: ")
        Value2 = Console.ReadLine()

        Addition = Value1 + Value2
    End Function

    Sub Main()
        Dim Result As String
        Dim Number1, Number2 As Double

        Result = Addition(Number1, Number2)
        Console.Write("{0} + {1} = {2}", Number1, Number2, Result)

        Console.WriteLine()
    End Sub

End Module

Here is an example of running the program:

Enter First Number:  224.58
Enter Second Number: 6068.14
0 + 0 = 6292.72

Notice that, although the values of the arguments were changed in 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 function. 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:


Module Module1

    Function Addition#(ByRef Value1 As Double, ByRef Value2 As Double)
        Console.Write("Enter First Number:  ")
        Value1 = Console.ReadLine()
        Console.Write("Enter Second Number: ")
        Value2 = Console.ReadLine()

        Addition = Value1 + Value2
    End Function

    Sub Main()
        Dim Result As String
        Dim Number1, Number2 As Double

        Result = Addition(Number1, Number2)
        Console.Write("{0} + {1} = {2}", Number1, Number2, Result)

        Console.WriteLine()
    End Sub

End Module

Here is an example of running the program:

Enter First Number:  224.58
Enter Second Number: 6068.14
224.58 + 6068.14 = 6292.72

Using this technique, you can pass as many arguments by reference and as many arguments by value as you want. As you may 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:
     
    
    Module Module1
    
        Sub RectangleDefinition(ByRef RectLength As Double, ByRef RectWidth As Double)
            Console.Write("Enter Rectangle Length: ")
            RectLength = Console.ReadLine()
            Console.Write("Enter Rectangle Width:  ")
            RectWidth = Console.ReadLine()
        End Sub
    
        Function RectPerimeter#(ByVal dblLen As Double, ByVal dblWid As Double)
            RectPerimeter = (dblLen + dblWid) * 2
        End Function
    
        Function RectArea#(ByVal dblLen As Double, ByVal dblWid As Double)
            RectArea = dblLen * dblWid
        End Function
    
    
        Function Main() As Integer
            Dim dblLength, dblWidth As Double
            Dim Perimeter, Area As Double
    
            RectangleDefinition(dblLength, dblWidth)
            Perimeter = RectPerimeter(dblLength, dblWidth)
            Area = RectArea(dblLength, dblWidth)
    
            Console.WriteLine("Rectangle Characteristics")
            Console.WriteLine("Length:  {0:F}", dblLength)
            Console.WriteLine("Width:   {0:F}", dblWidth)
            Console.WriteLine("Perimer: {0:F}", Perimeter)
            Console.WriteLine("Area:    {0:F}", Area)
    
            Console.WriteLine()
    	Return 0
        End Function
    
    End Module
  2. Save the file
  3. Compile and execute the application 
  4. Return to Notepad and close it
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next