Chapter 5 General Procedures 5 1 Sub Procedures
Chapter 5 - General Procedures • • • 5. 1 Sub Procedures, Part I 5. 2 Sub Procedures, Part II 5. 3 Function Procedures 5. 4 Modular Design 5. 5 A Case Study: Weekly Payroll Chapter 5 - VB 2008 by Schneider 1
5. 1 Sub Procedures, Part I • Sub Procedures • Variables and Expressions as Arguments • Calling Other Sub Procedures Chapter 5 - VB 2008 by Schneider 2
Devices for Modularity • Visual Basic has two devices for breaking problems into smaller pieces: • Sub procedures • Function procedures Chapter 5 - VB 2008 by Schneider 3
Sub Procedures • Perform one or more related tasks • General syntax Sub Procedure. Name() statements End Sub Chapter 5 - VB 2008 by Schneider 4
Calling a Sub Procedure • The statement that invokes a Sub procedure is also referred to as a Call statement. • A Call statement looks like this: Procedure. Name() Chapter 5 - VB 2008 by Schneider 5
Naming Sub Procedures • The rules for naming Sub procedures are the same as the rules for naming variables. Chapter 5 - VB 2008 by Schneider 6
Example lst. Box. Items. Clear() Explain. Purpose() lst. Box. Items. Add("") Sub Explain. Purpose() lst. Box. Items. Add("Program displays a sentence") lst. Box. Items. Add("identifying a sum. ") End Sub Chapter 5 - VB 2008 by Schneider 7
Passing Values • You can send values to a Sub procedure Sum(2, 3) Sub Sum(By. Val num 1 As Double, By. Val num 2 As Double) lst. Box. Items. Add("The sum of " & num 1 & " and " _ & num 2 & " is " & (num 1 + num 2) & ". " End Sub • In the Sum Sub procedure, 2 will be stored in num 1 and 3 will be stored in num 2 Chapter 5 - VB 2008 by Schneider 8
Arguments and Parameters • Sum(2, 3) arguments parameters Sub Sum(By. Val num 1 As Double, By. Val num 2 As Double) displayed automatically Chapter 5 - VB 2008 by Schneider 9
Several Calling Statements Explain. Purpose() Sum(2, 3) Sum(4, 6) Sum(7, 8) Output: Program The sum displays of 2 and of 4 and of 7 and a 3 6 8 sentence identifying a sum. is 5. is 10 is 15. Chapter 5 - VB 2008 by Schneider 10
Passing Strings and Numbers • Demo("CA", 38) Sub Demo(By. Val state As String, By. Val pop As Double) txt. Box, Text = state & " has population " & pop & _ " million. " End Sub • Note: The statement Demo(38, "CA") would not be valid. The types of the arguments must be in the same order as the types of the parameters. Chapter 5 - VB 2008 by Schneider 11
Variables and Expressions as Arguments Dim s As String = "CA" Dim p As Double = 19 Demo(s, 2 * p) Sub Demo(By. Val state As String, By. Val pop As Double) txt. Box. Text = state & " has population " & pop & _ " million. " End Sub • Note: The variable names in the arguments need not match the parameter names. For instance, s versus state. . Chapter 5 - VB 2008 by Schneider 12
Calling A Sub procedure can call another Sub procedure. Private Sub btn. Add_Click(. . . ) Handles btn. Add. Click Sum(2, 3) End Sub Sum(By. Val num 1 As Double, By. Val num 2 As Double) Display. Purpose() lst. Box. Items. Add("The sum of " & num 1 & " and " _ & num 2 & " is " & (num 1 + num 2) & ". " End Sub Chapter 5 - VB 2008 by Schneider 13
5. 2 Sub Procedures, Part II • • Passing by Value Passing by Reference Lifetime and Scope of a Variable Debugging Chapter 5 - VB 2008 by Schneider 14
By. Val and By. Ref • Parameters in Sub procedure headers are proceeded by By. Val or By. Ref • By. Val stands for By Value • By. Ref stands for By Reference Chapter 5 - VB 2008 by Schneider 15
Passing by Value • When a variable argument is passed to a By. Val parameter, just the value of the argument is passed. • After the Sub procedure terminates, the variable has its original value. Chapter 5 - VB 2008 by Schneider 16
Example Public Sub btn. One_Click (. . . ) Handles _ btn. One. Click Dim n As Double = 4 Triple(n) txt. Box. Text = CStr(n) End Sub Triple(By. Val num As Double) num = 3 * num End Sub Output: 4 Chapter 5 - VB 2008 by Schneider 17
Same Example: n num Public Sub btn. One_Click (. . . ) Handles _ btn. One. Click Dim num As Double = 4 Triple(num) txt. Box. Text = CStr(num) End Sub Triple(By. Val num As Double) num = 3 * num End Sub Output: 4 Chapter 5 - VB 2008 by Schneider 18
Passing by Reference • When a variable argument is passed to a By. Ref 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. Chapter 5 - VB 2008 by Schneider 19
Example Public Sub btn. One_Click (. . . ) Handles _ btn. One. Click Dim num As Double = 4 Triple(num) txt. Box. Text = CStr(num) End Sub Triple(By. Ref num As Double) num = 3 * num End Sub Output: 12 Chapter 5 - VB 2008 by Schneider 20
Example: num n Private Sub btn. One_Click(. . . ) Handles _ btn. One_Click Dim n As Double = 4 Triple(n) txt. Box. Text = CStr(n) End Sub Triple(By. Ref num As Double) num = 3 * num End Sub Output: 12 Chapter 5 - VB 2008 by Schneider 21
Lifetime and Scope of a Variable • Lifetime: Period during which it remains in memory. • Scope: In Sub procedures, defined same as in event procedures. • Suppose a variable is declared in procedure A that calls procedure B. While procedure B executes, the variable is alive, but of scope. Chapter 5 - VB 2008 by Schneider 22
Debugging • Programs with Sub procedures are easier to debug • Each Sub procedure can be checked individually before being placed into the program Chapter 5 - VB 2008 by Schneider 23
5. 3 Function Procedures • User-Defined Functions Having Several Parameters • User-Defined Functions Having No Parameters • User-Defined Boolean-valued Functions • Comparing Function Procedures with Sub Procedures • Named Constants Chapter 5 - VB 2008 by Schneider 24
Some Built-In Functions Function Example Input Output Int(2. 6) is 2 number Math. Round(1. 23, 1) is 1. 2 number, number Format. Percent(. 12) is 12. 00% number string Format. Number(123 45. 628, 1) is 12, 345. 6 number, number string Chapter 5 - VB 2008 by Schneider 25
Function Procedures • Function procedures (aka user-defined functions) always return one value • Syntax: Function. Name(By. Val var 1 As Type 1, _ By. Val var 2 As Type 2, _ …) As data. Type statement(s) Return expression End Function Chapter 5 - VB 2008 by Schneider 26
Example: Form txt. Full. Name txt. First. Name Chapter 5 - VB 2008 by Schneider 27
Example: Code Private Sub btn. Determine_Click(. . . ) _ Handles btn. Determine. Click Dim name As String name = txt. Full. Name. Text Function txt. First. Name. Text = First. Name(name) call End Sub Function First. Name(By. Val name As String) As String Dim first. Space As Integer first. Space = name. Index. Of(" ") Return statement Return name. Substring(0, first. Space) End Function Chapter 5 - VB 2008 by Schneider 28
Example: Form txt. Side. One txt. Side. Two txt. Hyp Chapter 5 - VB 2008 by Schneider 29
Example: Code Private Sub btn. Calculate_Click(. . . ) _ Handles btn. Calculate. Click Dim a, b As Double a = CDbl(txt. Side. One. Text) b = CDbl(txt. Side. Two. Text) txt. Hyp. Text = CStr(Hypotenuse(a, b)) End Sub Function Hypotenuse(By. Val a As Double, _ By. Val b As Double) As Double Return Math. Sqrt(a ^ 2 + b ^ 2) End Function Chapter 5 - VB 2008 by Schneider 30
User-Defined Function Having No Parameters Private Sub btn. Display_Click(. . . ) _ Handles btn. Display. Click txt. Box. Text = Saying() End Sub Function Saying() As String Dim str. Var As String str. Var = Input. Box("What is your" _ & " favorite saying? ") Return str. Var End Function Chapter 5 - VB 2008 by Schneider 31
Comparing Function Procedures with Sub Procedures • Subs are accessed using a Call statement • Functions are called where you would expect to find a literal or expression • For example: • result = function. Call • lst. Box. Items. Add (function. Call) Chapter 5 - VB 2008 by Schneider 32
Functions vs. Procedures • Both can perform similar tasks • Both can call other subs and functions • Use a function when you want to return one and only one value Chapter 5 - VB 2008 by Schneider 33
5. 4 Modular Design • Top-Down Design • Structured Programming • Advantages of Structured Programming Chapter 5 - VB 2008 by Schneider 34
Design Terminology • Large programs can be broken down into smaller problems • "divide-and-conquer" approach called "stepwise refinement" • Stepwise refinement is part of top-down design methodology Chapter 5 - VB 2008 by Schneider 35
Top-Down Design • General problems are at the top of the design • Specific tasks are near the end of the design • Top-down design and structured programming are techniques to enhance programmers' productivity Chapter 5 - VB 2008 by Schneider 36
Top-Down Design Criteria 1. The design should be easily readable and emphasize small module size. 2. Modules proceed from general to specific as you read down the chart. 3. The modules, as much as possible, should be single minded. That is, they should only perform a single well-defined task. 4. Modules should be as independent of each other as possible, and any relationships among modules should be specified. Chapter 5 - VB 2008 by Schneider 37
Top-Level Design HIPO Chart Chapter 5 - VB 2008 by Schneider 38
Detailed HIPO Chart Chapter 5 - VB 2008 by Schneider 39
Structured Programming • Control structures in structured programming: • Sequences: Statements are executed one after another. • Decisions: One of two blocks of program code is executed based on a test for some condition. • Loops (iteration): One or more statements are executed repeatedly as long as a specified condition is true. Chapter 5 - VB 2008 by Schneider 40
Advantages of Structured Programming • Goal to create correct programs that are easier to • write • understand • modify • "GOTO –less" programming Chapter 5 - VB 2008 by Schneider 41
Comparison of Flow Charts Chapter 5 - VB 2008 by Schneider 42
Easy to Write • Allows programmer to first focus on the big picture and take care of the details later • Several programmers can work on the same program at the same time • Code that can be used in many programs is said to be reusable Chapter 5 - VB 2008 by Schneider 43
Easy to Debug • Procedures can be checked individually • A driver program can be set up to test modules individually before the complete program is ready • Using a driver program to test modules (or stubs) is known as stub testing Chapter 5 - VB 2008 by Schneider 44
Easy to Understand • Interconnections of the procedures reveal the modular design of the program. • The meaningful procedure names, along with relevant comments, identify the tasks performed by the modules. • The meaningful variable names help the programmer to recall the purpose of each variable. Chapter 5 - VB 2008 by Schneider 45
Easy to Change • Because a structured program is selfdocumenting, it can easily be deciphered by another programmer. Chapter 5 - VB 2008 by Schneider 46
Object-Oriented Programming • an encapsulation of data and code that operates on the data • objects have properties, respond to methods, and raise events. Chapter 5 - VB 2008 by Schneider 47
- Slides: 47