Chapter8 General Procedures 1 General Procedures Function Procedure

  • Slides: 42
Download presentation
Chapter#8 General Procedures 1

Chapter#8 General Procedures 1

General Procedures Function Procedure Sub Procedures, Part II Modular Design 2

General Procedures Function Procedure Sub Procedures, Part II Modular Design 2

Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces:

Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Function procedures Sub procedures 3

Function Procedures User-Defined Functions Having One Parameter User-Defined Functions Having Several Parameters User-Defined Functions

Function Procedures User-Defined Functions Having One Parameter User-Defined Functions Having Several Parameters User-Defined Functions Having No Parameters User-Defined Boolean-Valued Functions 4

Some Built-In Functions Function: Int Example: Int(2. 6) is 2 Input: number Output: number

Some Built-In Functions Function: Int Example: Int(2. 6) is 2 Input: number Output: number Function: Math. Round Example: Math. Round(1. 23, 1) is 1. 2 Input: number, number Output: number 5

Some Built-In Functions (continued) Function: Format. Percent Example: Format. Percent(0. 12, 2) is 12.

Some Built-In Functions (continued) Function: Format. Percent Example: Format. Percent(0. 12, 2) is 12. 00% Input: number, number Output: string Function: Format. Number Example: Format. Number(12. 62, 1) is 12. 6 Input: number, number Output: string 6

Function Procedures Function procedures (also known as user-defined functions) always return one value Syntax:

Function Procedures Function procedures (also known as 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 Return. Data. Type statement(s) Return expression End Function 7

Example With One Parameter Function Fto. C(By. Val t As Double) As Double 'Convert

Example With One Parameter Function Fto. C(By. Val t As Double) As Double 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) End Function 8

Header of the Fto. C Function Procedure 9

Header of the Fto. C Function Procedure 9

Example 1: Form txt. Temp. F txt. Temp. C 10

Example 1: Form txt. Temp. F txt. Temp. C 10

Example 1: Code Using Function Private Sub Convert(. . . ) Handles btn. Cnvrt.

Example 1: Code Using Function Private Sub Convert(. . . ) Handles btn. Cnvrt. Click Dim f. Temp, c. Temp As Double f. Temp = txt. Temp. F. Text c. Temp = Fto. C(f. Temp) txt. Temp. C. Text = c. Temp End Sub Function Fto. C(By. Val t As Double) As Double Return (5 / 9) * (t - 32) End Function 11

Example 1: Code Without Using Function Private Sub btn. Convert_Click(. . . ) _Handles

Example 1: Code Without Using Function Private Sub btn. Convert_Click(. . . ) _Handles btn. Convert. Click Dim f. Temp, c. Temp As Double f. Temp = txt. Temp. F. Text c. Temp = (5 / 9) * (f. Temp - 32) txt. Temp. C. Text = c. Temp End Sub 12

Example 1: Output 13

Example 1: Output 13

User-Defined Function Having Several Parameters Function Pay(By. Val wage As Double, By. Val hrs

User-Defined Function Having Several Parameters Function Pay(By. Val wage As Double, By. Val hrs As Double) As Double Dim amt As Double ‘ Total amount of salary per hour Select Case hrs Case Is <= 40 amt = wage * hrs Case Is > 40 ‘the wage (salary/hour) increases 50% per every extra hour (extra hours >40) amt = wage * 40 +(0. 5 * wage * (hrs – 40)) End Select Return amt End Function 14

Example 3: Form txt. Wage txt. Hours txt. Earnings 15

Example 3: Form txt. Wage txt. Hours txt. Earnings 15

Example 3: Partial Code Private Sub btn. Calculate_Click(. . . ) _ Handles btn.

Example 3: Partial Code Private Sub btn. Calculate_Click(. . . ) _ Handles btn. Calculate. Click Dim hourly. Wage, hours. Workded As Double hourly. Wage = txt. Wage. Text hours. Worked = txt. Hours. Text txt. Earnings. Text = Format. Currency(Pay(hourly. Wage, hours. Worked)) End Sub Function call 16

Example 3: Output 17

Example 3: Output 17

User-Defined Function Having No Parameters Function Cost. Of. Item() As Double Dim price As

User-Defined Function Having No Parameters Function Cost. Of. Item() As Double Dim price As Double = CDbl(txt. Price. Text) Dim quantity As Integer = CDbl(txt. Quantity. Text) Dim cost = price * quantity Return cost End Function 18

User-Defined Boolean-Valued Function Is. Vowel. Word(By. Val word As String) As Boolean If word.

User-Defined Boolean-Valued Function Is. Vowel. Word(By. Val word As String) As Boolean If word. Index. Of("A") = -1 Then Return False End If. . If word. Index. Of("U") = -1 Then Return False End If Return True End Function 19

Sub Procedures, Part I Defining and Calling Sub Procedures Variables and Expressions as Arguments

Sub Procedures, Part I Defining and Calling Sub Procedures Variables and Expressions as Arguments Sub Procedures Calling Other Sub Procedures 20

General Form of Sub Procedure 21

General Form of Sub Procedure 21

Calling a Sub Procedure The statement that invokes /calls a Sub procedure is referred

Calling a Sub Procedure The statement that invokes /calls a Sub procedure is referred to as a calling statement. A calling statement looks like this: Procedure. Name(arg 1, arg 2, . . . , arg. N) 22

Naming Sub Procedures The rules for naming Sub procedures are the same as the

Naming Sub Procedures The rules for naming Sub procedures are the same as the rules for naming variables. 23

Passing Values Display. Sum( 2, 3 ) Sub Display. Sum(By. Val num 1 As

Passing Values Display. Sum( 2, 3 ) Sub Display. Sum(By. Val num 1 As Double, By. Val num 2 _ As Double) Dim z As Double z = num 1 + num 2 lst. Output. Items. Add("The sum of " & num 1 & " and " & num 2 & " is " & z & ". ") End Sub In the Sub procedure, 2 will be stored in num 1 and 3 will be stored in num 2 24

Arguments and Parameters Sum(2, 3) arguments parameters Sub Display. Sum(By. Val num 1 As

Arguments and Parameters Sum(2, 3) arguments parameters Sub Display. Sum(By. Val num 1 As Double, By. Val num 2 _ As Double) displayed automatically 25

Several Calling Statements Display. Sum(2, 3) Display. Sum(4, 6) Display. Sum(7, 8) Output: The

Several Calling Statements Display. Sum(2, 3) Display. Sum(4, 6) Display. Sum(7, 8) Output: The sum of 2 and 3 is 5. The sum of 4 and 6 is 10 The sum of 7 and 8 is 15. 26

Passing Strings and Numbers Demo("CA", 38) Sub Demo(By. Val state As String, By. Val

Passing Strings and Numbers Demo("CA", 38) Sub Demo(By. Val state As String, By. Val pop _ As Double) lst. Output. Items. Add = 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. 27

Variables and Expressions as Arguments Dim s As String = "CA" Dim p As

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) lst. Output. Items. Add = state & " has population " & pop & " million. " End Sub Note: The argument names need not match the parameter names. For instance, s versus state. 28

Sub Procedure Having No Parameters Sub Describe. Task() lst. Box. Items. Clear() lst. Box.

Sub Procedure Having No Parameters Sub Describe. Task() lst. Box. Items. Clear() lst. Box. Items. Add("This program displays") lst. Box. Items. Add("the name and population") lst. Box. Items. Add("of a state. ") End Sub 29

Sub Procedure Calling Another Sub Procedure Private Sub btn. Display_Click(. . . ) Handles

Sub Procedure Calling Another Sub Procedure Private Sub btn. Display_Click(. . . ) Handles _ btn. Display. Click Demo("CA", 37) End Sub Demo(By. Val state As String, By. Val pop _ As Double) Describe. Task() lst. Output. Items. Add("") lst. Output. Items. Add = state & " has population " & pop & " million. " End Sub 30

Output This program displays the name and population of a state. CA has population

Output This program displays the name and population of a state. CA has population 37 million. 31

Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return

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 32

By. Val and By. Ref Parameters in Sub procedure headers are proceeded by By.

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 33

Passing by Value When a variable argument is passed to a By. Val parameter,

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 ends, the variable has its original value. 34

Example Public Sub btn. One_Click (. . . ) Handles _btn. One. Click Dim

Example Public Sub btn. One_Click (. . . ) Handles _btn. One. Click Dim n As Double = 4 Triple(n) txt. Box 1. Text = “n = “ & n End Sub Triple(By. Val num As Double) num = 3 * num txt. Box 2. Text = “num = “ & num End Sub Output: Argument name (n) is different than parameter name (num) Memory (RAM): 12 4 num n num = 12 n = 4 35

Same Example: n num Public Sub btn. One_Click (. . . ) Handles _btn.

Same Example: n num Public Sub btn. One_Click (. . . ) Handles _btn. One. Click Dim num As Double = 4 Triple(num) txt. Box 1. Text = “ 2. num = “ & num End Sub Triple(By. Val num As Double) num = 3 * num txt. Box 2. Text = “ 1. num = “ & num End Sub Output: Argument name (num) is same as the parameter name (num) Memory (RAM): 12 4 num 1. num = 12 2. num = 4 36

Passing by Reference When a variable argument is passed to a By. Ref parameter,

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. 37

Example Public Sub btn. One_Click (. . . ) Handles _ btn. One. Click

Example Public Sub btn. One_Click (. . . ) Handles _ btn. One. Click Dim num As Double = 4 Triple(num) txt. Box 2. Text = “ 2. num = “ & num Argument name (num) is the same as parameter name End Sub (num) Sub Triple(By. Ref num As Double) num = 3 * num txt. Box 1. Text = “ 1. num = “ & num End Sub Output: 1. num = 12 Memory (RAM): 4 X 12 num 2. num = 12 38

Example: num n Private Sub btn. One_Click(. . . ) Handles _btn. One_Click Dim

Example: num n Private Sub btn. One_Click(. . . ) Handles _btn. One_Click Dim n As Double = 4 Triple(n) txt. Box 1. Text = “n = “ & num End Sub Triple(By. Ref num As Double) num = 3 * num txt. Box 1. Text = “num = “ & num End Sub Argument name (n) is different than parameter name (num) Memory (RAM): Output: 4 X num = 12 num n = 12 12 n 39

Most Common Use of By. Ref: Get Input (Read Input) Sub Input. Data(By. Ref

Most Common Use of By. Ref: Get Input (Read Input) Sub Input. Data(By. Ref wage As Double, By. Ref hrs As Double) wage = CDbl(txt. Wage. Text) hrs = CDbl(txt. Hours. Text) End Sub 40

Lifetime and Scope of a Variable Lifetime: Period during which it remains in memory.

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. 41

Functions vs. Sub procedures Both can perform similar tasks Both can call other procedures

Functions vs. Sub procedures Both can perform similar tasks Both can call other procedures Use a function when you want to return one and only one value 42