+ All Categories
Home > Documents > Chapter 5 - General Procedures

Chapter 5 - General Procedures

Date post: 20-Jan-2016
Category:
Upload: ronda
View: 47 times
Download: 3 times
Share this document with a friend
Description:
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 - PowerPoint PPT Presentation
Popular Tags:
21
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
Transcript
Page 1: Chapter 5 - General Procedures

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

Page 2: Chapter 5 - General Procedures

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

Page 3: Chapter 5 - General Procedures

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)

Page 4: Chapter 5 - General Procedures

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.

Page 5: Chapter 5 - General Procedures

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

4

btnOne Memory

num 4

Sub Triple Memory

num 412

Page 6: Chapter 5 - General Procedures

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

4

btnOne Memory

n 4

Sub Triple Memory

num 412

Page 7: Chapter 5 - General Procedures

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.

Page 8: Chapter 5 - General Procedures

8

Passing ByRefPublic Sub btnOne_Click (...)

Dim num As Double = 4 Triple(num) txtBox.Text = CStr(num)End Sub

Sub Triple(ByRef num As Double) num = 3 * numEnd Sub

Output: 12

Shared Memory

num 412

Page 9: Chapter 5 - General Procedures

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

n 412

Page 10: Chapter 5 - General Procedures

Common Use of SubProcedures

Sub btnCompute_Click(…)

InputData ( 2 way pass )

ProcessData (1 way and 2 way pass)

OutputData (1 way pass)

End Sub

10

Page 11: Chapter 5 - General Procedures

Most Common Use of ByRef: Get Input

Sub InputData(ByRef wage As Double,

ByRef hrs As Double)

wage = CDbl(txtWage.Text)

hrs = CDbl(txtHours.Text)

End Sub

11

Page 12: Chapter 5 - General Procedures

Sub Procedures that Return a Single Value with ByRef

• Should be avoided

• Can be replaced with a Function procedure

12

Page 13: Chapter 5 - General Procedures

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)

Page 14: Chapter 5 - General Procedures

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.

14

Page 15: Chapter 5 - General Procedures

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.

15

Page 16: Chapter 5 - General Procedures

16

Debugging

• Programs with Sub procedures are easier to debug

• Each Sub procedure can be checked individually before being placed into the program

Page 17: Chapter 5 - General Procedures

Step Into (F8) and ? To Print

17

Page 18: Chapter 5 - General Procedures

?n and ?num

18

Page 19: Chapter 5 - General Procedures

Debug with aFunction that calls a Function

19

Page 20: Chapter 5 - General Procedures

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)

Page 21: Chapter 5 - General Procedures

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.

Please read the online PDF comparing functions and sub procedures.


Recommended