210 likes | 401 Views
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
E N D
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 • Debugging
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)
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.
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
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
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.
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
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
Common Use of SubProcedures Sub btnCompute_Click(…) InputData ( 2 way pass ) ProcessData (1 way and 2 way pass) OutputData (1 way pass) End Sub
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
Sub Procedures that Return a Single Value with ByRef • Should be avoided • Can be replaced with a Function procedure
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)
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.
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.
Debugging • Programs with Sub procedures are easier to debug • Each Sub procedure can be checked individually before being placed into the program
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)
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.