Arrays and Procedures |
|
Arrays and Procedures |
Introduction |
Because an array is primarily treated as a variable, it shares the regular characteristics of other objects. For example, an array can be passed as argument and a function can return an array. The main difference is that an array by itself represents a group of items while a regular variable considers only its own value. |
Practical Learning: Introducing Arrays and Procedures |
Public Class IceCream Public Const BasePrice As Double = 1.55 Public Sub ProcessAnOrder() Dim dFlv As Integer, dCont As Integer, dIngrd As Integer Dim Scoops As Integer Dim PriceIngredient As Double, TotalPrice As Double Dim Flavor() As String = {"Vanilla", "Cream of Cocoa", "Organic Strawberry", _ "Butter Pecan", "Cherry Coke", "Chocolate Brownies"} Dim Container(3) As String Container(0) = "Cone" Container(1) = "Cup" Container(2) = "Bowl" Dim Ingredient() As String = {"No Ingredient", "Peanuts", "M & M", "Cookies"} Console.WriteLine("Ice Cream Vendor Machine") Do Try Console.WriteLine("What type of flavor do you want?") Console.WriteLine("1 - {0}", Flavor(0)) Console.WriteLine("2 - {0}", Flavor(1)) Console.WriteLine("3 - {0}", Flavor(2)) Console.WriteLine("4 - {0}", Flavor(3)) Console.WriteLine("5 - {0}", Flavor(4)) Console.WriteLine("6 - {0}", Flavor(5)) Console.Write("Your Choice? ") dFlv = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If dFlv < 1 Or dFlv > 6 Then Console.WriteLine("Invalid Choice - Try Again!" & vbCrLf) End If Loop While dFlv < 1 Or dFlv > 6 Do Try Console.WriteLine("What type of container do you want?") Console.WriteLine("1 - {0}", Container(0)) Console.WriteLine("2 - {0}", Container(1)) Console.WriteLine("3 - {0}", Container(2)) Console.Write("Your Choice? ") dCont = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If dCont < 1 Or dCont > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While dCont < 1 Or dCont > 3 Do Try Console.WriteLine("Do you want an ingredient or not") Console.WriteLine("1 - {0}", Ingredient(0)) Console.WriteLine("2 - {0}", Ingredient(1)) Console.WriteLine("3 - {0}", Ingredient(2)) Console.WriteLine("4 - {0}", Ingredient(3)) Console.Write("Your Choice? ") dIngrd = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If dIngrd < 1 Or dIngrd > 4 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While dIngrd < 1 Or dIngrd > 4 Do Try Console.Write("How many scoops(1, 2, or 3)? ") Scoops = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If Scoops < 1 Or Scoops > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While Scoops < 1 Or Scoops > 3 If dIngrd = 2 Or dIngrd = 3 Or dIngrd = 4 Then PriceIngredient = 0.65 Else PriceIngredient = 0.0 End If TotalPrice = (BasePrice * Scoops) + PriceIngredient DisplayReceipt(dFlv, dCont, dIngrd, Scoops, TotalPrice) End Sub Public Sub DisplayReceipt(ByVal Flv As Integer, ByVal Cont As Integer, _ ByVal Ingrd As Integer, _ ByVal spoons As Integer, ByVal TotalPrice As Double) Console.WriteLine(vbCrLf & "Ice Cream Order") Select Case Flv Case 2 Console.WriteLine("Flavor: Cream of Cocoa") Case 3 Console.WriteLine("Flavor: Organic Strawberry") Case 4 Console.WriteLine("Flavor: Butter Pecan") Case 5 Console.WriteLine("Flavor: Cherry Coke") Case 6 Console.WriteLine("Flavor: Chocolate Brownies") Case Else Console.WriteLine("Flavor: Vavilla") End Select Select Case Cont Case 2 Console.WriteLine("Container: Cup") Case 3 Console.WriteLine("Container: Bowl") Case Else Console.WriteLine("Container: Cone") End Select Select Case Ingrd Case 2 Console.WriteLine("Ingredient: Peanuts") Case 3 Console.WriteLine("Ingredient: M & M") Case 4 Console.WriteLine("Ingredient: Cookies") Case Else Console.WriteLine("Ingredient: None") End Select Console.WriteLine("Scoops: {0}", spoons) Console.WriteLine("Total Price: {0:C}" & vbCrLf, TotalPrice) End Sub End Class |
Imports System Public Class Exercise Public Shared Sub main() Dim ic As IceCream = New IceCream ic.ProcessAnOrder() End Sub End Class
Ice Cream Vendor Machine What type of flavor do you want? 1 - Vanilla 2 - Cream of Cocoa 3 - Organic Strawberry 4 - Butter Pecan 5 - Cherry Coke 6 - Chocolate Brownies Your Choice? 2 What type of container do you want? 1 - Cone 2 - Cup 3 - Bowl Your Choice? 1 Do you want an ingredient or not 1 - No Ingredient 2 - Peanuts 3 - M & M 4 - Cookies Your Choice? 3 How many scoops(1, 2, or 3)? 2 Ice Cream Order Flavor: Cream of Cocoa Container: Cone Ingredient: M & M Scoops: 2 Total Price: $3.75
Passing a Member of an Array as Argument |
Each member of an array is considered a sub-variable that is, a variable in its own right with a value. Based on this, you can pass a single member of an array as argument. In this case, you can pass the name of the array variable with the accompanying index to a procedure. Here is an example:
Imports System Module Exercise Dim Names() As String = {"Hermine", "Gertrude", "Helene", "Paulette"} Private Sub ShowName(ByVal Name As String) Console.WriteLine("Name: {0}", Name) End Sub Public Sub main() ShowName(Names(2)) Console.WriteLine() End Sub End Module
This would produce:
Name: Helene
The main purpose of using an array is to use various pseudo-variables grouped under one name. Still, an array is primarily a variable. As such, it can be passed to a procedure and it can be returned from one. Like a regular variable, an array can be passed as argument. To do this, in the parentheses of a procedure, provide the data type, the empty square brackets, and the name of the argument. Here is an example: Imports System Module Exercise Private Sub DisplayNumbers(ByVal args() As Double) End Sub Public Sub main() End Sub End Module When an array has been passed to a procedure, it can be used in the body of the procedure as any array can be, following the rules of array variables. For example, you can display its values. Because an array is derived from the Array class, an array passed as argument carries its number of members. This means that you don't have to pass an additional argument. The simplest way you can use an array is to display the values of its members. This could be done as follows: Imports System Module Exercise Private Sub DisplayNumbers(ByVal args() As Double) For Each arg As Double In args Console.WriteLine("Number: {0}", arg) Next End Sub Public Sub main() End Sub End Module To call a procedure that takes an array as argument, simply type the name of the array in the parentheses of the called procedure. Here is an example: Imports System Module Exercise Private Sub DisplayNumbers(ByVal args() As Double) For Each arg As Double In args Console.WriteLine("Number: {0}", arg) Next End Sub Public Sub main() Dim number(4) As Double number(0) = 12.44 number(1) = 525.38 number(2) = 6.28 number(3) = 2448.32 number(4) = 632.04 DisplayNumbers(number) Console.WriteLine() End Sub End Module This would produce: Number: 12.44 Number: 525.38 Number: 6.28 Number: 2448.32 Number: 632.04 When an array is passed as argument to a procedure, the array is passed by reference. This means that, if the procedure makes any change to the array, the change would be kept when the procedure exits. This is illustrated in the following program: Imports System Module Exercise Private Sub DisplayNumber(ByVal args() As Double) For Each arg As Double In args Console.WriteLine("Number: {0}", arg) Next args(0) = -888.64 args(1) = 24.57 args(2) = 60450.24 args(3) = -6.4 args(4) = 900.002 End Sub Public Sub main() Dim Numbers(4) As Double numbers(0) = 12.44 numbers(1) = 525.38 numbers(2) = 6.28 Numbers(3) = 2448.32 Numbers(4) = 632.04 Console.WriteLine("=-= First Call =-=") For Each nbr As Double In Numbers Console.WriteLine("Number: {0}", nbr) Next Console.WriteLine() Console.WriteLine("=-= External Call =-=") DisplayNumber(Numbers) Console.WriteLine() Console.WriteLine("=-= Second Call =-=") For Each nbr As Double In Numbers Console.WriteLine("Number: {0}", nbr) Next Console.WriteLine() End Sub End Module This would produce: =-= First Call =-= Number: 12.44 Number: 525.38 Number: 6.28 Number: 2448.32 Number: 632.04 =-= External Call =-= Number: 12.44 Number: 525.38 Number: 6.28 Number: 2448.32 Number: 632.04 =-= Second Call =-= Number: -888.64 Number: 24.57 Number: 60450.24 Number: -6.4 Number: 900.002 In the Main function, the array is primarily initialized and its values are displayed to the screen. The array is then passed to an external procedure. That procedure receives the array ByVal. It displays the values of the array as they were passed it. Before exiting, the procedure modifies the array and closes. Back in Main(), the array is displayed without receiving any modification. But the values of the array appear different. This is because the array was modified by the called procedure. This illustrates that an array is passed by reference even if you don't use the ByRef keyword on the argument. You can use this characteristic to initialize an array from a procedure. Here is an example: Imports System Module Exercise Private Sub InitializeNumbers(ByVal args() As Double) args(0) = -888.64 args(1) = 24.57 args(2) = 60450.24 args(3) = -6.4 args(4) = 900.002 End Sub Private Sub DisplayNumbers(ByVal nbrs() As Double) For Each nbr As Double In nbrs Console.WriteLine("Number: {0}", nbr) Next End Sub Public Sub main() Dim Numbers(4) As Double InitializeNumbers(Numbers) DisplayNumbers(Numbers) Console.WriteLine() End Sub End Module Notice that the InitializeList() procedure receives an un-initialized array but returns it with new values. Because an array is passed by reference, if you want to explicitly indicate this, when passing an array, you can accompany it with the ByRef keyword. Here is an example: Imports System Module Exercise Private Sub InitializeNumbers(ByRef args() As Double) args(0) = 542.28 args(1) = 64.96 args(2) = 4750.25 args(3) = 28.44 args(4) = 18500.45 End Sub Private Sub DisplayNumbers(ByVal nbrs() As Double) For Each nbr As Double In nbrs Console.WriteLine("Number: {0}", nbr) Next End Sub Public Sub main() Dim Numbers(4) As Double InitializeNumbers(Numbers) DisplayNumbers(Numbers) Console.WriteLine() End Sub End Module This would produce: Number: 542.28 Number: 64.96 Number: 4750.25 Number: 28.44 Number: 18500.45 |
|
Practical Learning: Passing an Array as Argument |
Public Class IceCream Public Const BasePrice As Double = 1.55 Private Sub CreateFlavors(ByVal Flavors() As String) Flavors(0) = "Vanilla" Flavors(1) = "Cream of Cocoa" Flavors(2) = "Organic Strawberry" Flavors(3) = "Butter Pecan" Flavors(4) = "Cherry Coke" Flavors(5) = "Chocolate Brownies" End Sub Private Sub CreateIngredients(ByVal Ingredients() As String) Ingredients(0) = "No Ingredient" Ingredients(1) = "Peanuts" Ingredients(2) = "M & M" Ingredients(3) = "Cookies" End Sub Private Function ChooseContainer(ByVal Containers() As String) As Integer Dim choice As Integer = 1 Do Try Console.WriteLine("What type of container do you want?") For i As Integer = 0 To 2 Console.WriteLine("{0} - {1}", i + 1, Containers(i)) Next Console.Write("Your Choice? ") choice = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If choice < 1 Or choice > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While choice < 1 Or choice > 3 Return choice End Function Private Function RequestFlavor(ByVal Flavors() As String) As Integer Dim choice As Integer = 1 Do Try Console.WriteLine("What type of flavor do you want?") For i As Integer = 0 To 5 Console.WriteLine("{0} - {1}", i + 1, Flavors(i)) Next Console.Write("Your Choice? ") choice = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If choice < 1 Or choice > 6 Then Console.WriteLine("Invalid Choice - Try Again!" & vbcrlf) End If Loop While choice < 1 Or choice > 6 Return choice End Function Private Function RequestIngredient(ByVal Ingredients() As String) As Integer Dim choice As Integer = 1 Do Try Console.WriteLine("Do you want an ingredient or not") For i As Integer = 0 To 3 Console.WriteLine("{0} - {1}", i + 1, Ingredients(i)) Next Console.Write("Your Choice? ") choice = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If choice < 1 Or choice > 4 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While choice < 1 Or choice > 4 Return choice End Function Private Function GetTheNumberOfScoops() As Integer Dim number As Integer = 1 Do Try Console.Write("How many scoops(1, 2, or 3)? ") number = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If number < 1 Or number > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While number < 1 Or number > 3 Return number End Function Public Sub ProcessAnOrder() Dim dFlv As Integer, dCont As Integer, dIngrd As Integer Dim Scoops As Integer Dim PriceIngredient As Double, PriceScoop As Double, TotalPrice As Double Dim Flavors(6) As String Dim Containers(3) As String Dim Ingredients(4) As String Containers(0) = "Cone" Containers(1) = "Cup" Containers(2) = "Bowl" CreateFlavors(Flavors) CreateIngredients(Ingredients) dFlv = RequestFlavor(Flavors) dCont = ChooseContainer(Containers) dIngrd = RequestIngredient(Ingredients) Scoops = GetTheNumberOfScoops() If dIngrd = 2 Or dIngrd = 3 Or dIngrd = 4 Then PriceIngredient = 0.5 Else PriceIngredient = 0.0 End If If Scoops = 1 Then PriceScoop = 0.65 ElseIf Scoops = 2 Then PriceScoop = 1.05 Else PriceScoop = 1.55 End If Console.WriteLine("Ice Cream Vendor Machine") TotalPrice = BasePrice + PriceScoop + PriceIngredient DisplayReceipt(Flavors(dFlv - 1), Containers(dCont - 1), _ Ingredients(dIngrd - 1), Scoops, TotalPrice) End Sub Public Sub DisplayReceipt(ByVal Flv As String, ByVal Cont As String, _ ByVal Ingrd As String, _ ByVal spoons As Integer, ByVal TotalPrice As Double) Console.WriteLine(vbCrLf & "Ice Cream Order") Console.WriteLine("Flavor: {0}", Flv) Console.WriteLine("Container: {0}", Cont) Console.WriteLine("Ingredient: {0}", Ingrd) Console.WriteLine("Scoops: {0}", spoons) Console.WriteLine("Total Price: {0:C}" & vbCrLf, TotalPrice) End Sub End Class |
What type of flavor do you want? 1 - Vanilla 2 - Cream of Cocoa 3 - Organic Strawberry 4 - Butter Pecan 5 - Cherry Coke 6 - Chocolate Brownies Your Choice? 0 Invalid Choice - Try Again! What type of flavor do you want? 1 - Vanilla 2 - Cream of Cocoa 3 - Organic Strawberry 4 - Butter Pecan 5 - Cherry Coke 6 - Chocolate Brownies Your Choice? 7 Invalid Choice - Try Again! What type of flavor do you want? 1 - Vanilla 2 - Cream of Cocoa 3 - Organic Strawberry 4 - Butter Pecan 5 - Cherry Coke 6 - Chocolate Brownies Your Choice? 5 What type of container do you want? 1 - Cone 2 - Cup 3 - Bowl Your Choice? 0 Invalid Choice - Try Again! What type of container do you want? 1 - Cone 2 - Cup 3 - Bowl Your Choice? 3 Do you want an ingredient or not 1 - No Ingredient 2 - Peanuts 3 - M & M 4 - Cookies Your Choice? 0 Invalid Choice - Try Again! Do you want an ingredient or not 1 - No Ingredient 2 - Peanuts 3 - M & M 4 - Cookies Your Choice? 8 Invalid Choice - Try Again! Do you want an ingredient or not 1 - No Ingredient 2 - Peanuts 3 - M & M 4 - Cookies Your Choice? 4 How many scoops(1, 2, or 3)? 2 Ice Cream Vendor Machine Ice Cream Order Flavor: Cherry Coke Container: Bowl Ingredient: Cookies Scoops: 2 Total Price: $3.10 |
A Function that Returns an Array |
In the previous section, we saw that a (sub) procedure could return an array because if it modifies an array it receives as argument, the array keeps the new value. Still, you can use a function to formally return an array. When declaring or defining the function, you must specify its data type. To create a function that returns an array, on the left of the function's name, provide the type of value that the returned array will be made of, followed by empty parentheses. Here is an example: Private Function InitializeValues() As Double() End Function Remember that a function must always return an appropriate value depending on how it was declared. In this case, if it was specified as returning an array, then make sure it returns an array and not a regular variable. One way you can do this is to declare and possibly initialize a local array variable. After using the local array, you return only its name (without the parentheses). Here is an example: Private Function InitializeValues() As Double() Dim Values(4) As Double Values(0) = 542.28 Values(1) = 64.96 Values(2) = 4750.25 Values(3) = 28.44 Values(4) = 18500.45 InitializeValues = Values End Function When a function returns an array, that function can be assigned to an array declared locally when you want to use it. Remember to initialize a variable with such a function only if the variable is an array. If you initialize an array variable with a procedure that doesn't return an array, you would receive an error. Here is an example: |
Imports System Module Exercise Private Function InitializeValues() As Double() Dim Values(4) As Double Values(0) = 542.28 Values(1) = 64.96 Values(2) = 4750.25 Values(3) = 28.44 Values(4) = 18500.45 InitializeValues = Values End Function Private Sub DisplayNumbers(ByVal nbrs() As Double) For Each nbr As Double In nbrs Console.WriteLine("Number: {0}", nbr) Next End Sub Public Sub main() Dim Numbers(4) As Double Numbers = InitializeValues() DisplayNumbers(Numbers) Console.WriteLine() End Sub End Module
This would produce:
Number: 542.28 Number: 64.96 Number: 4750.25 Number: 28.44 Number: 18500.45
Practical Learning: Returning an Array From a Procedure |
Public Class IceCream Public Const BasePrice As Double = 1.55 Dim ChoiceFlavor As Integer Dim ChoiceContainer As Integer Dim ChoiceIngredient As Integer Dim Scoops As Integer Dim TotalPrice As Double Private Function PrepareFlavors() As String() Dim Flavors(10) As String Flavors(0) = "Vanilla" Flavors(1) = "Cream of Cocoa" Flavors(2) = "Chocolate Chip" Flavors(3) = "Organic Strawberry" Flavors(4) = "Butter Pecan" Flavors(5) = "Cherry Coke" Flavors(6) = "Chocolate Brownies" Flavors(7) = "Caramel Au Lait" Flavors(8) = "Chunky Butter" Flavors(9) = "Chocolate Cookie" Return Flavors End Function Private Sub CreateIngredients(ByRef Ingredients() As String) Ingredients(0) = "No Ingredient" Ingredients(1) = "Peanuts" Ingredients(2) = "M & M" Ingredients(3) = "Cookies" End Sub Private Sub ChooseContainer(ByRef Containers() As String) Containers(0) = "Cone" Containers(1) = "Cup" Containers(2) = "Bowl" Do Try Console.WriteLine("What type of container do you want?") For i As Integer = 0 To 2 Step 1 Console.WriteLine("{0} - {1}", i + 1, Containers(i)) Next Console.Write("Your Choice? ") ChoiceContainer = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If ChoiceContainer < 1 Or ChoiceContainer > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While ChoiceContainer < 1 Or ChoiceContainer > 3 End Sub Private Sub RequestFlavor(ByRef Flavors() As String) Flavors = PrepareFlavors() Do Try Console.WriteLine("What type of flavor do you want?") For i As Integer = 0 To 9 Step 1 Console.WriteLine("{0} - {1}", i + 1, Flavors(i)) Next Console.Write("Your Choice? ") ChoiceFlavor = CInt(Console.ReadLine()) Catch exc As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If ChoiceFlavor < 1 Or ChoiceFlavor > 10 Then Console.WriteLine("Invalid Choice - Try Again!" & vbCrLf) End If Loop While ChoiceFlavor < 1 Or ChoiceFlavor > 10 End Sub Private Sub ProcessIngredients(ByRef Ingredients() As String) CreateIngredients(Ingredients) Do Try Console.WriteLine("Do you want an ingredient or not") For i As Integer = 0 To 3 Step 1 Console.WriteLine("{0} - {1}", i + 1, Ingredients(i)) Next Console.Write("Your Choice? ") ChoiceIngredient = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If ChoiceIngredient < 1 Or ChoiceIngredient > 4 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While ChoiceIngredient < 1 Or ChoiceIngredient > 4 End Sub Private Sub GetTheNumberOfScoops() Do Try Console.Write("How many scoops(1, 2, or 3)? ") Scoops = CInt(Console.ReadLine()) Catch ex As InvalidCastException Console.WriteLine("You must enter a valid number and no other character!") End Try If Scoops < 1 Or Scoops > 3 Then Console.WriteLine("Invalid Choice - Try Again!") End If Loop While Scoops < 1 Or Scoops > 3 End Sub Public Sub ProcessAnOrder() Dim PriceIngredient As Double, PriceScoop As Double Dim Flavor(10) As String Dim Container(3) As String Dim Ingredient(4) As String RequestFlavor(Flavor) ChooseContainer(Container) ProcessIngredients(Ingredient) GetTheNumberOfScoops() If ChoiceIngredient = 2 Or ChoiceIngredient = 3 Or ChoiceIngredient = 4 Then PriceIngredient = 0.5 Else PriceIngredient = 0.0 End If If Scoops = 1 Then PriceScoop = 0.65 ElseIf Scoops = 2 Then PriceScoop = 1.05 Else PriceScoop = 1.55 End If Console.WriteLine("Ice Cream Vendor Machine") TotalPrice = BasePrice + PriceScoop + PriceIngredient DisplayReceipt(Flavor, Container, Ingredient) End Sub Public Sub DisplayReceipt(ByRef Flv() As String, ByRef Cont() As String, _ ByRef Ingrd() As String) Console.WriteLine(vbCrLf & "Ice Cream Order") Console.WriteLine("Flavor: {0}", Flv(ChoiceFlavor)) Console.WriteLine("Container: {0}", Cont(ChoiceContainer)) Console.WriteLine("Ingredient: {0}", Ingrd(ChoiceIngredient)) Console.WriteLine("Scoops: {0}", Scoops) Console.WriteLine("Total Price: {0:C}" & vbCrLf, TotalPrice) End Sub End Class |
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|