Passing Arrays to Procedures: ByVal vs. ByRef. Passing Arrays to Procedures Passing the Array – Specify the name of the array without using parentheses.

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

Pemrograman VisualMinggu …7… Page 1 MINGGU Ke Tujuh Pemrograman Visual Pokok Bahasan: Arrays Tujuan Instruksional Khusus: Mahasiswa dapat menjelaskan.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.
Arrays. Overview Overview of Arrays Creating Arrays Using Arrays.
Example 2.
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
 Pearson Education, Inc. All rights reserved Arrays.
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Why to Create a Procedure
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
 2009 Pearson Education, Inc. All rights reserved Arrays.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
6.1 Introduction Divide and Conquer –The best way to develop and maintain a large program is to construct it from small, manageable pieces. BZUPAGES.COM.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2002 Prentice Hall. All rights reserved. 1 Chapter 7 - Arrays Outline 7.1Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.
Visual Basic Programming I 56:150 Information System Design.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
5.1 Introduction Problem Solving –Requires understanding of: Building blocks Program-construction principles BZUPAGES.COM.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Subs and Functions Ch 6. Introduction VB.NET Procedures Sub Procedures Function Procedures Outline.
CHAPTER 07 Arrays and Vectors (part II). OBJECTIVES In this part you will learn:  To pass arrays to functions.  Basic searching techniques.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved. - Edited By: Maysoon Al-Duwais 1.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Arrays 1.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
Visual Basic I Programming
Chapter 7 - Arrays Outline 7.1 Introduction 7.2   Arrays 7.3   Declaring and Allocating Arrays 7.4   Examples Using Arrays   Allocating an Array.
Method.
JavaScript: Functions.
Advanced Programming Chapter 8: Arrays
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Arrays.
Procedures: Functions and Subroutines
Methods.
Presentation transcript:

Passing Arrays to Procedures: ByVal vs. ByRef

Passing Arrays to Procedures Passing the Array – Specify the name of the array without using parentheses – Every array object “knows” its own upper bound Do not need to pass the upper bound of the array as a separate argument Dim hourlyTemperatures As Integer() = New Integer(24) {} DayData(hourlyTemperatures) In Visual Basic, arrays always are passed by reference Receiving the array – The procedure’s parameter list must specify that an array will be received. Sub DayData(ByVal temperatureData As Integer())

Although entire arrays are always passed by reference, individual array elements can be passed in the same manner as simple variables of that type. For instance, array element values of primitive data types, such as Integer, can be passed by value or by reference, depending on the procedure definition. To pass an array element to a procedure, use the indexed name of the array element as an argument in the call to the procedure. The following program demonstrates the difference between passing an entire array and passing an array element. Passing Arrays to Procedures

1 ' Fig. 7.8: PassArray.vb 2 ' Passing arrays and individual array elements to procedures. 3 4 Imports System.Windows.Forms 5 6 Module modPassArray 7 Dim output As String 8 9 Sub Main() 10 Dim array1 As Integer() = New Integer() {1, 2, 3, 4, 5} 11 Dim i As Integer output = "EFFECTS OF PASSING ENTIRE ARRAY " & _ 14 "BY REFERENCE:" & vbCrLf & vbCrLf & _ 15 "The values of the original array are:" & vbCrLf ' display original elements of array1 18 For i = 0 To array1.GetUpperBound(0) 19 output &= " " & array1(i) 20 Next ModifyArray(array1) ' array is passed by reference 23

24 output &= vbCrLf & _ 25 "The values of the modified array are:" & vbCrLf ' display modified elements of array1 28 For i = 0 To array1.GetUpperBound(0) 29 output &= " " & array1(i) 30 Next output &= vbCrLf & vbCrLf & _ 33 "EFFECTS OF PASSING ARRAY ELEMENT " & _ 34 "BY VALUE:" & vbCrLf & vbCrLf & "array1(3) " & _ 35 "before ModifyElementByVal: " & array1(3) ' array element passed by value 38 ModifyElementByVal(array1(3)) output &= vbCrLf & "array1(3) after " & _ 41 "ModifyElementByVal: " & array1(3) output &= vbCrLf & vbCrLf & "EFFECTS OF PASSING " & _ 44 "ARRAY ELEMENT BY REFERENCE: " & vbCrLf & vbCrLf & _ 45 "array1(3) before ModifyElementByRef: " & array1(3) 46

47 ' array element passed by reference 48 ModifyElementByRef(array1(3)) output &= vbCrLf & "array1(3) after " & _ 51 "ModifyElementByRef: " & array1(3) MessageBox.Show(output, "Passing Arrays", _ 54 MessageBoxButtons.OK, MessageBoxIcon.Information) 55 End Sub ' Main ' procedure modifies array it receives (note ByVal) 58 Sub ModifyArray(ByVal arrayParameter As Integer()) 59 Dim j As Integer For j = 0 To arrayParameter.GetUpperBound(0) 62 arrayParameter(j) *= 2 63 Next End Sub ' ModifyArray 66

67 ' procedure modifies integer passed to it 68 ' original is not be modified (note ByVal) 69 Sub ModifyElementByVal(ByVal element As Integer) output &= vbCrLf & "Value received in " & _ 72 "ModifyElementByVal: " & element 73 element *= 2 74 output &= vbCrLf & "Value calculated in " & _ 75 "ModifyElementByVal: " & element 76 End Sub ' ModifyElementByVal ' procedure modifies integer passed to it 79 ' original is be modified (note ByRef) 80 Sub ModifyElementByRef(ByRef element As Integer) output &= vbCrLf & "Value received in " & _ 83 "ModifyElementByRef: " & element 84 element *= 2 85 output &= vbCrLf & "Value calculated in " & _ 86 "ModifyElementByRef: " & element 87 End Sub ' ModifyElementByRef End Module ' modPassArray

Passing Arrays: ByVal vs. ByRef ByVal ByVal – Causes the value of the argument to be copied to a local variable in the procedure – Changes to the local variable are reflected in the local copy of that variable, not in the original variable in the calling program – But if the argument is of a reference type, like an array, passing it ByVal actually passes it by reference, so changes to the object affect the original objects in the callers

1 ' Fig. 7.9: ArrayReferenceTest.vb 2 ' Testing the effects of passing array references using 3 ' ByVal and ByRef. 4 5 Module modArrayReferenceTest 6 7 Sub Main() 8 Dim i As Integer 9 10 ' declare array references 11 Dim firstArray As Integer() 12 Dim firstArrayCopy As Integer() ' allocate firstArray and copy its reference 15 firstArray = New Integer() {1, 2, 3} 16 firstArrayCopy = firstArray Console.WriteLine("Test passing array reference " & _ 19 "using ByVal.") 20 Console.Write("Contents of firstArray before " & _ 21 "calling FirstDouble: ") ' print contents of firstArray 24 For i = 0 To firstArray.GetUpperBound(0) 25 Console.Write(firstArray(i) & " ") 26 Next Prints contents first to verify that FirstDouble indeed changes the array’s contents Copies reference firstArray to variable firstArrayCopy, now they reference the same object Demonstrates the subtle difference between passing a reference ByVal vs. passing a reference ByRef.

27 28 ' pass firstArray using ByVal 29 FirstDouble(firstArray) Console.Write(vbCrLf & "Contents of firstArray after " & _ 32 "calling FirstDouble: ") ' print contents of firstArray 35 For i = 0 To firstArray.GetUpperBound(0) 36 Console.Write(firstArray(i) & " ") 37 Next ' test whether reference was changed by FirstDouble 40 If firstArray Is firstArrayCopy Then 41 Console.WriteLine(vbCrLf & "The references are " & _ 42 "equal.") 43 Else 44 Console.WriteLine(vbCrLf & "The references are " & _ 45 "not equal.") 46 End If ' declare array references 49 Dim secondArray As Integer() 50 Dim secondArrayCopy As Integer() 51 Compares references firstArray and firstArrayCopy VB provides operator Is for comparing references to determine whether they are referencing the same object. firstArray is passed to FirstDouble Reference is passed ByVal

52 ' allocate secondArray and copy its reference 53 secondArray = New Integer() {1, 2, 3} 54 secondArrayCopy = secondArray Console.WriteLine(vbCrLf & "Test passing array " & _ 57 "reference using ByRef.") 58 Console.Write("Contents of secondArray before " & _ 59 "calling SecondDouble: ") ' print contents of secondArray before procedure call 62 For i = 0 To secondArray.GetUpperBound(0) 63 Console.Write(secondArray(i) & " ") 64 Next ' pass secondArray using ByRef 67 SecondDouble(secondArray) Console.Write(vbCrLf & "Contents of secondArray " & _ 70 "after calling SecondDouble: ") ' print contents of secondArray after procedure call 73 For i = 0 To secondArray.GetUpperBound(0) 74 Console.Write(secondArray(i) & " ") 75 Next 76

77 ' test whether the reference was changed by SecondDouble 78 If secondArray Is secondArrayCopy Then 79 Console.WriteLine(vbCrLf & "The references are " & _ 80 "equal.") 81 Else 82 Console.WriteLine(vbCrLf & "The references are " & _ 83 "not equal.") 84 End If End Sub ' Main ' procedure modifies elements of array and assigns 89 ' new reference (note ByVal) 90 Sub FirstDouble(ByVal array As Integer()) 91 Dim i As Integer ' double each element value 94 For i = 0 To array.GetUpperBound(0) 95 array(i) *= 2 96 Next ' create new reference and assign it to array 99 array = New Integer() {11, 12, 13} 100 End Sub ' FirstDouble Allocates a new array, and attempts to assign it’s reference to parameter array, attempting to overwrite reference firstArray in memory, but will fail because the reference was passed ByVal Multiplies all the elements of the array by 2

' procedure modifies elements of array and assigns 103 ' new reference (note ByRef) 104 Sub SecondDouble(ByRef array As Integer()) 105 Dim i As Integer ' double contents of array 108 For i = 0 To array.GetUpperBound(0) 109 array(i) *= Next ' create new reference and assign it to array 113 array = New Integer() {11, 12, 13} 114 End Sub ' SecondDouble End Module ' modPassArray Because the reference was passed with ByRef, the called procedure has the ability to modify what the reference actually points to

Test passing array reference using ByVal. Contents of firstArray before calling FirstDouble: Contents of firstArray after calling FirstDouble: The references are equal. Test passing array reference using ByRef. Contents of secondArray before calling SecondDouble: Contents of secondArray after calling SecondDouble: The references are not equal.