1 / 21

Chapter 5 - General Procedures

Chapter 5 - General Procedures. 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design. 5.3 Sub Procedures, Part II. Passing by Value Passing by Reference Sub Procedures that Return a Single Value Lifetime and Scope of Variables and Constants

ronda
Download Presentation

Chapter 5 - General Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design

  2. 5.3 Sub Procedures, Part II • Passing by Value • Passing by Reference • Sub Procedures that Return a Single Value • Lifetime and Scope of Variables and Constants • Debugging

  3. ByVal and ByRef • Parameters in Sub procedure headers are proceeded by ByVal or ByRef • ByVal stands for By Value (one-way pass) • ByRef stands for By Reference (two-way pass)

  4. Passing ByVal(ue) • One-Way Pass • When a variable argument is passed to a ByVal parameter, • just the value of the argument is passed in. • Modified values are NOT passed back. • After the Sub procedure terminates, • The variable in the sub ceases to exist • the variable in the calling event only knows its original value.

  5. Passing ByVal: (One-Way) Public Sub btnOne_Click (...) Dim num As Double = 4 Triple(num) txtBox.Text = CStr(num) End Sub Sub Triple(ByVal num As Double) num = 3 * num End Sub Output:4 btnOne Memory num 4 4 Sub Triple Memory num 4 12

  6. Passing ByVal n num Public Sub btnOne_Click (...) Dim n As Double = 4 Triple(n) txtBox.Text = CStr(n) End Sub Sub Triple(ByVal num As Double) num = 3 * num End Sub Output:4 btnOne Memory n 4 4 Sub Triple Memory num 4 12

  7. Passing by Reference • When a variable argument is passed to a ByRef parameter, the parameter is given the same memory location as the argument. • After the Sub procedure terminates, the variable has the value of the parameter.

  8. Passing ByRef Public Sub btnOne_Click (...) Dim num As Double = 4 Triple(num) txtBox.Text = CStr(num) End Sub Sub Triple(ByRef num As Double) num = 3 * num End Sub Output: 12 Shared Memory 4 12 num

  9. Passing ByRef: num n Private Sub btnOne_Click(...) Handles Dim n As Double = 4 Triple(n) txtBox.Text = CStr(n) End Sub Sub Triple(ByRef num As Double) num = 3 * num End Sub Output:12 Shared Memory 4 12 n

  10. Common Use of SubProcedures Sub btnCompute_Click(…) InputData ( 2 way pass ) ProcessData (1 way and 2 way pass) OutputData (1 way pass) End Sub

  11. Most Common Use of ByRef: Get Input Sub InputData(ByRefwage As Double, ByRef hrs As Double) wage = CDbl(txtWage.Text) hrs = CDbl(txtHours.Text) End Sub

  12. Sub Procedures that Return a Single Value with ByRef • Should be avoided • Can be replaced with a Function procedure

  13. Lifetime of a Variable • Lifetime of a Variable: • is the period during which a variable remains in memory. • depends upon the location of where it is created (i.e. dim)

  14. Scope • Where the variable can be referenced. • Class Level • variables are known everywhere • Event procedures, sub procedures, function procedures • Local variables are only known within the procedure • byVal variables are only known within the procedure • byRef variables reference variables in the calling program and procedures • Special Statements • Variable created (i.e. dim) inside of an IF..END IF are only known inside of the IF statement being executed.

  15. Scope (Continued) • Sub procedures that call sub procedures “Suppose a variable is declared in procedure A that calls procedure B.” While procedure B executes, the variable is alive, but out of scope.

  16. Debugging • Programs with Sub procedures are easier to debug • Each Sub procedure can be checked individually before being placed into the program

  17. Step Into (F8) and ? To Print

  18. ?n and ?num

  19. Debug with aFunction that calls a Function

  20. Comparing Function Procedures with Sub Procedures • Sub procedures are accessed using a calling statement procedureCall • Functions are called where you would expect to find a literal or expression result = functionCall lstBox.Items.Add (functionCall)

  21. Functions vs. Procedures • Both can perform similar tasks • Both can call other procedures • Use a function when you want to return one and only one value • Use a sub procedure to return more than one values Please read the online PDF comparing functions and sub procedures.

More Related