Parameters and Arrays

17
arrays

Transcript of Parameters and Arrays

Page 1: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 1/21

arrays

Page 2: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 2/21

 Arrays defined

• Array refers to a series of data items arranged in a useful wausually referenced by an index.

• An array is a data structure, which allows a set of items of iddata type to be stored together using the same identifier na

• Arrays are declared in a similar way to standard variables, ex

the array size and dimensions are included.• For example, the following declares an array that reserves fi

locations in memory and labels these as ‘Names’: 

DIM Names(4) As String

Page 3: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 3/21

 Arrays defined cont’d 

• Each data item is called an element of the array. To referencparticular element a programmer must use the appropriate

• Arrays simplify the processing of similar data.

Page 4: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 4/21

One-dimensional arrays

• A one-dimensional array is a data structure in which the arrdeclared using a single index and can be visually represente

• The following diagram shows the visual representation of thNames(4):

Page 5: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 5/21

Two-dimensional arrays• A two-dimensional array is a data structure in which the arr

declared using two indices and can be visually represented a

• The following diagram shows the visual representation of anStudents(4,2):

Page 6: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 6/21

Initialising an array• Initialising an array is a procedure in which every value in the a

with starter values – this starting value would typically be “” foarray, or 0 for a numeric array.

• Initialisation needs to be done to ensure that the array does nresults from a previous use, elsewhere in the program.

• Algorithm for initialising a one-dimensional numeric array:

DIM TestScores(9) As Integer

DIM Index As Integer

FOR Index = 0 TO 9

TestScores(Index) = 0

NEXT

Page 7: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 7/21

TWO DIMENSION ARRAYS• Each individual element can be referenced by its row and co

indices.

• Algorithm for initialising a two-dimensional string array:

DIM Students(4,2) As String

DIM RowIndex, ColumnIndex As Integer

FOR RowIndex = 0 TO 4

FOR ColumnIndex = 0 TO 2Students(RowIndex,ColumnIndex) = “” 

NEXT

NEXT

Page 8: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 8/21

parametersPassing parameters

Page 9: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 9/21

Parameter defined• A parameter is a value that is ‘passed’ (given) to a subroutine (p

function).

• A parameter is a piece of data that is sent to a procedure when

• The data is used by the procedure to help it carry out its tasks

• The subroutine uses the value of the parameter within its execuaction of the subroutine will be different depending upon the pait is passed.

• Parameters are placed in parenthesis after the subroutine nameSquare(5) ‘passes the parameter 5 – returns 25

• The order and type of parameters that are passed to a function must match the parameters in the function or procedure heade

Page 10: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 10/21

Passing Parameters• The formal parameter links the data in the calling pr

(which will change) to its use in the function or proc

• Actual parameter/argument is the data item suppliepassed to the local variable

• Actual parameters can be passed to functions andprocedures either by value or by reference

Page 11: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 11/21

Important rules for passing paramet• The number of actual and formal parameters must be the same

• Parameters are matched according to their position, not accord

names• The data types of a matching pair of parameters must be the sa

• A reference parameter is used when a piece of data needs to bof the procedure

• A value parameter is a piece of data used by the procedure. An

made to it inside the procedure are not passed back to again.

Page 12: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 12/21

Passing by value• Only the value of the parameter is passed to the proc

called.

• Value of the parameter can be manipulated by the pcalled.

• When the procedure called is terminated the new vadiscarded and the value of the parameter in the calliprocedure returns to the original value

E l t ill t t i t

Page 13: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 13/21

Example to illustrate passing a parametervalue

Procedure1

Declare a

a = 5

Call Procedure2 (a)

Display a

EndProcedure1

Procedure2 (ByVal b)

b = b + 2

EndProcedure2

• The output of the parameter will be the display of value 5

Page 14: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 14/21

Passing by reference• The parameter is stored in the original location and

pointer (a reference to the parameter’s memory add

passed to the procedure called.

• Any changes made to the reference passed to theprocedure called, will remove the value of the paramthe original location.

• When the procedure called is terminated the new vavailable to the calling procedure

xamp e o us ra e pass ng a parame

Page 15: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 15/21

xamp e o us ra e pass ng a paramereferenceProcedure1

Declare a

a = 5

Call Procedure2 (a)

Display a

EndProcedure1

Procedure2 (ByRef b)

b = b + 2

EndProcedure2

• The output of the parameter will be the display of value 7

Page 16: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 16/21

VB illustration: passing by valuePrivate Sub cmdByVal_Click()

Dim number As Integer

number = txtNumber.Text

Call Display1(number)

Print number

End Sub

Public Sub Display1(ByVal num As Integer)num = num + 2

End Sub

Page 17: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 17/21

VB illustration: passing by referencePrivate Sub cmdOk_Click()

Dim number As Integer

number = txtNumber.Text

Call Display(number)

Print number

End Sub

Public Sub Display(ByRef num As Integer)

num = num + 2

End Sub

Page 18: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 18/21

What is the difference between pa

value and pass by reference?

P i k

Page 19: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 19/21

Pair work

Design a VB program which finds the smanumber between two numbers. You are s

to use parameter passing in your code.

Hint: if statement may be used in the pubprocedure to find the smaller number wh

must be passed to the procedure calling

Page 20: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 20/21

summary• A parameter is a piece of data that is sent to a procedu

called

• Actual parameters are passed to the procedure and arethe formal parameters which are part of the procedure’declaration. The data type of a matching pair of actual aparameters must be the same

• Formal parameters are divided into value and reference

parameters. Any changes made to a reference parametprocedure are passed out again. Changes to a value parcannot be passed out again.

S ti d

Page 21: Parameters and Arrays

8/11/2019 Parameters and Arrays

http://slidepdf.com/reader/full/parameters-and-arrays 21/21

Summary continued• You do not have to declare formal parameters as value(

reference (ByRef ). If you don’t, Visual Basic defaults to r

• When you pass a parameter by reference when it shoul

value no errors occur, but if you pass by value when it sbeen by reference, a serious error will occur.