Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters

2 Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce variable and procedure scope, and passing parameters/arguments to procedures Objectives, by end of this week’s sessions, you should be able to: –Determine whether a variable or procedure is in or out of scope at a given point in a piece of code –Appropriately Select a variable’s scope in their own program –Use parameters in their own program to make procedures more generic

3 Mark Dixon, SoCCE SOFT 131Page 3 Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Form/module/unit (general declarations) Things that have scope: –Variables (covered previously) –Procedures (current focus) –Functions (subject of next lecture)

4 Mark Dixon, SoCCE SOFT 131Page 4 Variable Scope (How) Module variables –general declarations (top) Local variables: –in procedures Option Explicit Dim mv as long Private Sub btnCalc_Click() Dim lv1 as long... End Sub Private Sub btnAdd_Click() Dim lv2 As Long... End Sub

5 Mark Dixon, SoCCE SOFT 131Page 5 Scope (why) In short – Robustness of code/software –Protection from accidental outside interference One of many responses to code that is –Difficult to maintain, and –Unreliable –House of cards phenomenon Prevent: –Uncontrolled and ad hoc interactions between code Always define things at lowest level needed

6 Mark Dixon, SoCCE SOFT 131Page 6 Exercise 1: Variable Scope In the following: Option Explicit Private Sub btnCalc_Click() Dim x As Integer x = 0 lblTotal.Caption = "£" & x End Sub Private Sub btnQuit_Click() x = 0 lblTotal.Caption = "£" & x End Sub Variable not found error

7 Mark Dixon, SoCCE SOFT 131Page 7 Exercise 2: Variable Scope Will this compile? Option Explicit … Dim x As integer … Private Sub thing() Dim z As Integer x = 23 y = "there" z = 12 end Private Sub btnTest_Click() Dim y As String y = "hello" x = 67 z = 53 End Sub Is x in scope? Is y in scope? Is z in scope? Is y in scope? Is x in scope? Is z in scope? Yes No Yes No

8 Mark Dixon, SoCCE SOFT 131Page 8 Example 1: Counter Option Explicit Dim Counter As Long Private Sub Form_Load() Counter = 0 Me.lblCounter.Caption = Counter End Sub Private Sub btnReset_Click() Counter = 0 Me.lblCounter.Caption = Counter End Sub Private Sub btnUp_Click() Counter = Counter + 1 Me.lblCounter.Caption = Counter End Sub Private Sub btnDown_Click() Counter = Counter - 1 Me.lblCounter.Caption = Counter End Sub Counter

9 Mark Dixon, SoCCE SOFT 131Page 9 Parameters/Arguments (what) Sometimes procedures need information –Making a cup of tea: milk, and number of sugars Makes them more flexible Make cup of tea Milk Sugars

10 Mark Dixon, SoCCE SOFT 131Page 10 Parameters (how) Procedure Declaration –each parameter needs name & type Procedure Call –list values for parameters in order Option Explicit Private Sub Calc(Num1 As Long, Num2 As long) lblResult.Caption = Num1 * Num2 End Sub Private Sub btnCalc_Click() Calc txtNum1.Text, txtNum2.Text End Sub

11 Mark Dixon, SoCCE SOFT 131Page 11 Exercise 3: Parameters Given the following declaration: What will the following put in lblOutput? Sub thing(Num1 As Long, Num2 As Long, Num3 As Long) Dim tmpOutput As Long tmpOutput = (Num1 + Num2) * Num3 lblOutput.Caption = tmpOutput End Sub thing 2, 3, 6 thing 6, 3, 2 thing 20, 5, 2 30 18 50

12 Mark Dixon, SoCCE SOFT 131Page 12 Example 2: Till v1 Option Explicit Dim SubTotal As Double Dim Total As Double Private Sub btnEquals_Click() Dim price As Double Dim quantity As Double price = Val(Me.txtPrice.Text) quantity = Val(Me.txtQuantity.Text) SubTotal = price * quantity lblSubTotal.Caption = "£" & SubTotal End Sub Private Sub btnAdd_Click() Total = Total + SubTotal lblTotal.Caption = "£" & Total End Sub Private Sub btnUndo_Click() Total = Total - SubTotal lblTotal.Caption = "£" & Total End Sub Till v1

13 Mark Dixon, SoCCE SOFT 131Page 13 amount: Double Example 3: Till v2 Option Explicit Dim SubTotal As Double Dim Total As Double Private Sub TotalAdd(amount As Double) Total = Total + amount Me.lblTotal.Caption = "£" & Total End Sub Private Sub btnEquals_Click() Dim price As Double Dim quantity As Double price = Val(Me.txtPrice.Text) quantity = Val(Me.txtQuantity.Text) SubTotal = price * quantity lblSubTotal.Caption = "£" & SubTotal End Sub Private Sub btnAdd_Click() TotalAdd SubTotal End Sub Private Sub btnUndo_Click() TotalAdd -SubTotal End Sub TotalAdd Till v2

14 Mark Dixon, SoCCE SOFT 131Page 14 Example 4: Heart Rate v3 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub DrawHR() Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) Next End Sub Private Sub btnDraw_Click() DrawHR End Sub

15 Mark Dixon, SoCCE SOFT 131Page 15 Example 5: Heart Rate v4 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub DrawHR(picTemp As PictureBox) Dim i As Long picTemp.Cls For i = 0 To 6 picTemp.Line -(i * 500, HR(i) * 10) Next End Sub Private Sub btnDraw_Click() DrawHR Me.picMain1 DrawHR Me.picMain2 End Sub


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters."

Similar presentations


Ads by Google