190 likes | 260 Views
14 – Passing parameters by reference, and Modules. Session Aims & Objectives. Aims To introduce the idea of passing by reference the idea of modules Objectives, by end of this week’s sessions, you should be able to: pass parameters by reference
E N D
Session Aims & Objectives • Aims • To introduce the idea of passing by reference • the idea of modules • Objectives,by end of this week’s sessions, you should be able to: • pass parameters by reference • appropriately split a program into multiple modules
Variables and Memory Addresses • The computer keeps track of where variables are stored in memory by using memory addresses. • Every byte (position) in memory has a memory address: • In the above example the variable identified by the name x is stored at location 63542 (this is the address of the first byte of data allocated to the variable x) 0 63542 x 23 Integer Identifier Type Value Memory
Parameter Passing Methods • There are 2 ways to pass parameters to functions and procedures: • Passing by Value: a literalvalue is passed from the call to the definition (you have already used this)Sub p1(x As integer) …End Sub • Passing by Reference: a variable’s memory address (a reference to the variables position in memory) is passed from the call to the definitionSub p2(ByRef y As integer) …End Sub
Why pass by reference? • It allows the value of the passed variable to be changed • i.e. it allows functions and procedures to change the value of things passed to them • Normally parameters are for input data – only functions can output data via the return value • Pass by reference allows data to be input and output via parameters
Example: Change the Value Dimy As Integer Dim b As integer Sub P1(x As integer) x = x * 2 End Sub Sub P2(ByRef x AS integer) x = x * 2 End Sub y = 11 b = 12 P1 y' What is the value of y? P2b ' What is the value of b? Pass by Ref
What can be passed • Pass by value – both literals and variables can be passed (variables are substituted by their value)p1 y ' This is fine. p1 21 ' This is fine. • Pass by reference – only variables can be passed (in fact the variable’s memory address is passed)literals cannot be passed – they have no memory addressp2 y ' This is fine. p2 21 ' This generates an error.
Example: Pass by Ref vs. Function P1 var x: integer F1 x: integer integer Sub P2(ByRef x AsInteger) x = x * 2 End Sub Function F2(ByVal x As integer) AsInteger F2 = x * 2 End Function Dim b As integer b = 4 P2b ' What is the value of b? b = 4 b = F2(b) ' What is the value of b?
Pass by Ref vs. Function • a procedure that changes the value of a single parameter is equivalent to a function, • the procedure P2:P2bwas equivalent to: • the function F2:b = F2(b) • However, • F2 is far more explicit, • P2 is a bit cryptic: not obvious that value of b changes • this makes code difficult to read, which can lead to errors
Example: Total Dim Nums(1 To 5)AsInteger Dim tot As integer Function Total() AsInteger Dim tmpTot As integer Dimi As integer tmpTot = 0 For i = 1 to 5 tmpTot = tmpTot + Nums(i) Next Total = tmpTot End Function Nums(1) = 23 Nums(2) = 17 Nums(3) = 28 Nums(4) = 12 Nums(5) = 25 tot = Total() ' What is the value of tot?
Example: Average Dim ave AsDouble Function Average() AsDouble Dim tmpTot AsInteger Dim i AsInteger tmpTot = 0 for i = 1 to 5 tmpTot = tmpTot + Nums(i) Next Average = tmpTot / 5 End Function ave = Average() ' What is the value of ave?
Two results? integer (total) TotAve double (average) • Total and Average functions share a lot of code • Useful to combine them • Problem: • a function can only have 1 output • This:is not possible (in VB, or Delphi anyway)
Example: Total and Average SubTotAve(ByRef T As Integer, _ ByRef A As Double) Dim I As integer T = 0 For i = 1 to 5 T = T + Nums(i) Next A = T / 5 End Sub tot = 0 ave = 0 TotAve tot, ave ' What is the value of ave and tot? var T: integer TotAve var A: double
Multiple Modules Forms • Projects can contain many modules/units • form modules (*.FRM) • Click the Project menu • Click the Add Form menu item • code modules (*.BAS) • Click the Project menu • Click the Add Module menu item • Modules • divide your code into separate parts • available to other forms and code modules
Public & Private • Private – can only be used in current module • Public – can be used by any module • Used for: • module level variables (instead of dim) Private x As Integer • procedures and functions (start of declaration) Private Sub Display() … End Sub
Example: Employees v3 frmMain modEmployees Option Explicit Private Sub Form_Load() curEmp = 1 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Option Explicit Private Type TEmployee Surname As String Forenames As String Salary As Double End Type Dim Employees(1 To 10) As TEmployee Public curEmp As Integer Sub EmpDisplay() frmMain.lblEmpNum.Caption = curEmp frmMain.txtSurname.Text = Employees(curEmp).Surname frmMain.txtForenames.Text = Employees(curEmp).Forenames frmMain.txtSalary.Text = Employees(curEmp).Salary End Sub Sub EmpStore() Employees(curEmp).Surname = frmMain.txtSurname.Text Employees(curEmp).Forenames = frmMain.txtForenames.Text Employees(curEmp).Salary = Val(frmMain.txtSalary.Text) End Sub Employees v3
Multiple Forms: Start Form • To set the start form: • Click the Project menu • Click the Properties menu item • Click the General tab • Click the Start up object list • Select a form
Example: Multiple Forms • Show method – displays form • Hide method – hides form
Modules: Sharing Project A Project B Module 1 Module 2 Form 1 Form 2 Form 3 Form 4 • Can share modules between projects: • Click the File menu • Click the Add File menu item • Select the module file • Press the [Return] key