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

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Constants and Variables.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Chapter 41 Sub Procedures, Part II (Continue). Chapter 42 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Types and Loops.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
Arrays1 From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
Mark Dixon Page 1 15 – Structured Programming. Mark Dixon Page 2 Admin: Coursework 3 – Test In class test –9 Feb 2010 –2 Feb 2010: revision (technique)
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 15 – Structured Programming. Mark Dixon 2 Admin: Test 2 In class test –11 Feb 2014 –4 Feb 2014: revision (technique) session 50 mins short.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon 1 Soft051 Examination Sample Questions.
CSC 162 Visual Basic I Programming. Storage Classes Determines the “lifetime” of an identifier Types: –Automatic Default Memory is allocated for the variable.
B065: PROGRAMMING Variables 2. Starter  What is wrong with the following piece of code: Option Explicit Off Dim num1 as string Dim num2 as integer num1.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
11 – Enumerated Data Types, & Procedure Parameters
Visual Basic 6 Programming.
Presentation transcript:

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

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

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)

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

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

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

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

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

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

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

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,

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

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

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

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