Download presentation
Presentation is loading. Please wait.
Published byJuliana Marlene Quinn Modified over 9 years ago
2
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures
3
Chapter 4 - Visual Basic Schneider2 Outline & Objective u Sub Procedures u Procedure Parameters u Function Procedures u Modular Design
4
Chapter 4 - Visual Basic Schneider3 What is a procedure? u A general procedure is a set of commands that is given a name so that it can be invoked by another part of the program u Procedures make a program easier to develop, test, and correct
5
Chapter 4 - Visual Basic Schneider4 General Procedures u In Visual Basic, there are two types of procedures: –Sub –Function u Note: To distinguish procedures from event procedures, Sub and Function procedures are referred to as general procedures.
6
Chapter 4 - Visual Basic Schneider5 Sub Procedures Properties: u may be called u may be passed data called arguments u may return values to the calling program u may change the data stored in a received variable
7
Chapter 4 - Visual Basic Schneider6 Passing Arguments to Subs: u When you define a Sub procedure, sometimes you need to transfer variables that are used in different Subs. This is called passing in programming languages.
8
Chapter 4 - Visual Basic Schneider7 Components of Sub Procedure: u name: used to identify the Sub procedure u parameters: a Sub procedure accepts values from the caller through its parameters. It may also send values back to the caller through its parameters.
9
Chapter 4 - Visual Basic Schneider8 Sub Procedure's Name u The rules for naming Sub Procedures are the same as naming variables. u In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names.
10
Chapter 4 - Visual Basic Schneider9 Syntax of a Sub Procedure Private Sub ProcedureName ( ) statement(s) End Sub
11
Chapter 4 - Visual Basic Schneider10 Creating Visual Basic Sub Procedure: u Activate the code window u Select Add Procedure from the Tools menu u Type in the name of the Sub procedure u Click on Private in the Scope box u Press the Enter key or click the OK button u Type the statements of the Sub procedure into the code window
12
Chapter 4 - Visual Basic Schneider11 Example of Call to a Sub Procedure: Private Sub cmdCompute_Click() Dim num As Single num = Val(InputBox("Enter a number:")) Call Triple(num) End Sub
13
Chapter 4 - Visual Basic Schneider12 Sub Procedure Triple: Private Sub Triple(num As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * num End Sub Private Sub Triple(par As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * par End Sub
14
Chapter 4 - Visual Basic Schneider13 Passing Arguments to Sub Procedures u Arguments : Variables or expressions placed in parentheses in a Call statement. u Not only is the value of the argument passed to the parameter, but the value of the parameter is passed back to the argument.
15
Chapter 4 - Visual Basic Schneider14 Example of Arguments u Call Triple(num)
16
Chapter 4 - Visual Basic Schneider15 Parameters u Variables placed in parentheses after a Sub Procedure's name. u When the procedure is called, the values of the corresponding arguments are placed in the parameters.
17
Chapter 4 - Visual Basic Schneider16 Example of Parameters u Private Sub Triple(num As Single)
18
Chapter 4 - Visual Basic Schneider17 Passing arguments to parameters Call Triple(num) Private Sub Triple (num As Single) Argument Parameter
19
Chapter 4 - Visual Basic Schneider18 Passing Arguments to Parameters u Call Add (x, y ) u Private Sub Add ( num1 As Single, num2 As Single) Parameters Arguments
20
Chapter 4 - Visual Basic Schneider19 Passing Arguments u A Sub procedure receives the location of the arguments, and may use and modify the value of the arguments stored at that location. u Two-way into and out of the Sub Procedure.
21
Chapter 4 - Visual Basic Schneider20 Passing Arguments Private Sub cmdDisplay_Click() Dim amt As Single amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub
22
Chapter 4 - Visual Basic Schneider21 Sub Triple Private Sub Triple(num As Single) ' Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub
23
Chapter 4 - Visual Basic Schneider22 Passing Arguments Call Triple(amt) Private Sub Triple (num As Single) amt num
24
Chapter 4 - Visual Basic Schneider23 Passing Data - by Reference 2266 amt num
25
Chapter 4 - Visual Basic Schneider24 Important Rules for Passing Arguments to a Sub u The number of arguments and parameters must match. u The data type of each argument must match its corresponding parameter.
26
Chapter 4 - Visual Basic Schneider25 Local Variables: u A variable that is used only in a specific procedure (Sub or Function). u The scope of the local variable is the Sub or Function in which that variable has been defined.
27
Chapter 4 - Visual Basic Schneider26 Local Variables: u Declared within a procedure definition u Private to a procedure definition u Variables in different procedures are totally independent u Different procedures can have variables with the same names; however, each variable will have its own memory location
28
Chapter 4 - Visual Basic Schneider27 Advantages of Local Variables u Extremely useful for team programming u Protects against accidental side effects (change of the value of the variable in one procedure that causes an error in another)
29
Chapter 4 - Visual Basic Schneider28 Example of Local Variables Private Sub cmdButton_Click() Dim var1 As Integer, var2 As Integer,num As Integer var1 = 2 var2 = 4 Call Add(num) picBox.Print num End Sub
30
Chapter 4 - Visual Basic Schneider29 Sub Add Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub
31
Chapter 4 - Visual Basic Schneider30 Form-Level Variables u Form-level variables are visible to every procedure. u Form-level variables appear at the top of the code window.
32
Chapter 4 - Visual Basic Schneider31 How to create Form-Level Variables? u Activate the code window u Click on the down arrow to the right of the object list box u Click on General u Click on Declaration in the procedure list box u Type in Dim statements for form-level variables
33
Chapter 4 - Visual Basic Schneider32 Example ' In Declaration section of General Dim num1 As Single, num2 As Single
34
Chapter 4 - Visual Basic Schneider33 Common Errors u Passing incorrect data types. u Not returning the result of the computation back to the calling program.
35
Chapter 4 - Visual Basic Schneider34 Another Example Private Sub cmdDisplay_Click() ' Demonstrate that variables in a Sub procedure do ' not retain their values in subsequent calls Call Three End Sub
36
Chapter 4 - Visual Basic Schneider35 Sub Three Private Sub Three() Dim num As Single ' Display the value of num and assign it the value 3 picResults.Print num; num = 3 End Sub
37
Chapter 4 - Visual Basic Schneider36 What is a function? u Similar to a Sub, performs a specific task u Unlike a Sub, returns a single value to the calling program
38
Chapter 4 - Visual Basic Schneider37 Types of Functions u Standard functions (built-in) u User-defined functions
39
Chapter 4 - Visual Basic Schneider38 User-Defined Function u A function returns a single value u The value is returned by assigning a value to the name of the function u The values of the parameters of a function should not be used for returning values from a function
40
Chapter 4 - Visual Basic Schneider39 The Function Syntax Private Function FunctionName(parameter-list) As dataType Statement(s)…… ….. FunctionName = value End Function u Note: value must be of the type dataType specified in the function declaration
41
Chapter 4 - Visual Basic Schneider40 Example of a Function (using a function to change from Fahrenheit to Celsius, Reading the value from a text box) Private Sub cmdConvert_Click() picTempC.Cls picTempC.Print FtoC(Val(txtTempF.Text)) End Sub Private Function FtoC(t As Single) As Single ' Convert Fahrenheit temperature to Celsius FtoC = (5 / 9) * (t - 32) End Function
42
Chapter 4 - Visual Basic Schneider41 Example of a Function (using a function to change from Fahrenheit to Celsius, Reading the value from an input box) Private Sub cmdConvert_Click() picTempC.Cls picTempC.Print FtoC(Val(InputBox("please enter a value to convert"))) End Sub Private Function FtoC(t As Single) As Single ' Convert Fahrenheit temperature to Celsius FtoC = (5 / 9) * (t - 32) End Function
43
Chapter 4 - Visual Basic Schneider42 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) notice the difference from the previous slide. Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
44
Chapter 4 - Visual Basic Schneider43 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will not give a correct result because we used ( ) around c in the argument list Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), (c)) picTempC.Print c End Sub Private Sub FtoC(f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
45
Chapter 4 - Visual Basic Schneider44 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will not give a correct result because we used ByVal in the parameter list Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, ByVal c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
46
Chapter 4 - Visual Basic Schneider45 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) This example will give a correct result because we used ByRef in the parameter list (removing ByRef will have the same effect) Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(f As Single, ByRef c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
47
Chapter 4 - Visual Basic Schneider46 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) what about this?! Private Sub cmdConvert_Click() Dim c As Single picTempC.Cls Call FtoC(Val(InputBox("please enter a value to convert")), c) picTempC.Print c End Sub Private Sub FtoC(ByVal f As Single, c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
48
Chapter 4 - Visual Basic Schneider47 Example of a Sub (using a sub to change from Fahrenheit to Celsius, Reading the value from an input box) what about this too?! Private Sub cmdConvert_Click() Dim c As Single Dim f As Single picTempC.Cls f=Val(InputBox("please enter a value to convert")) Call FtoC( f, c) picTempC.Print “The Celsius value of the Fahrenheit ” ; f ; “ is ” ; c End Sub Private Sub FtoC(ByVal f As Single, ByRef c As Single) ' Convert Fahrenheit temperature to Celsius c = (5 / 9) * (f - 32) End Sub
49
Chapter 4 - Visual Basic Schneider48 Rules for Defining and Calling a Function u User-defined function must include a statement that assigns the function name a value. u User-defined functions are called in the same way that built-in functions are called. u A user-defined function may be called in an expression.
50
Chapter 4 - Visual Basic Schneider49 Returning Value A function can receive many values Only one value can be directly returned
51
Chapter 4 - Visual Basic Schneider50 Example of a Function Private Sub cmdDetermine_Click() Dim nom As String nom = FirstName(txtFullName.Text) picFirstName.Print "The first name is “; nom End Sub Private Function FirstName(nom As String) As String Dim firstSpace As Integer firstSpace = InStr(nom, " ") FirstName = Left(nom, firstSpace - 1) End Function
52
Chapter 4 - Visual Basic Schneider51 Common Errors u Passing incorrect data types u Not specifying the data type of the returned value u Not assigning a value to the Function name inside the Function definition u Misspelling of the Function name
53
Chapter 4 - Visual Basic Schneider52 Further Examples Private Sub cmdDisplay_Click() Call DisplayVolume(1, 2) Call DisplayVolume(3, 4) End Sub Private Sub DisplayVolume(r As Single, h As Single) PicOutput.Print "Volume of cylinder with base area "; Area(r) PicOutput.Print "and height"; h; "is"; h * Area(r) End Sub Private Function Area(r As Single) As Single Area = 3.14159 * r ^ 2 End Function
54
Chapter 4 - Visual Basic Schneider53 Another Example Private Sub cmdDisplay_Click() Dim a As String a = “Choo ” picOutput.Print = TypeOfTrain() End Sub Private Function TypeOfTrain() As String Dim a As String a = a & a TypeOfTrain = a & “Train” End Function Output: Train
55
Chapter 4 - Visual Basic Schneider54 Another Example Private Sub cmdDisplay_Click() Dim num As Single num = 5 picOutput.Print Triple (num) picOutput.Print num End Sub Private Function Triple(x As Single) As Single Dim num As Single num = 3 Triple = num * x End Function Output: 15 5
56
Chapter 4 - Visual Basic Schneider55 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(x As Integer, y As Integer) x = 11 y = 16 End Sub Output 10 15 11 16
57
Chapter 4 - Visual Basic Schneider56 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef x As Integer, ByRef y As Integer) x = 11 y = 16 End Sub Output 10 15 11 16
58
Chapter 4 - Visual Basic Schneider57 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef x As Integer, ByVal y As Integer) x = 11 y = 16 End Sub Output 10 15 11 15
59
Chapter 4 - Visual Basic Schneider58 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByVal x As Integer, ByVal y As Integer) x = 11 y = 16 End Sub Output 10 15
60
Chapter 4 - Visual Basic Schneider59 ByRef and ByVal Examples Private Sub cmdCall_Click() Dim a As Integer Dim b As Integer a = 10 b = 15 Print a; b Call mySub(a, b) Print a; b End Sub Sub mySub(ByRef y As Integer, ByRef x As Integer) x = 11 y = 16 End Sub Output 10 15 16 11
61
More Arithmetic Operators Chapter 4 - Visual Basic Schneider60 u (num1 \ num2) is the Quotient of num1 divided by num2 u (num1 Mod num2) is the whole number remainder of num1 divided by num2 17 / 2 = 8.517 \ 2 = 817 Mod 2 = 1 16 / 2 = 816\ 2 =816 Mod 2 = 0 34/7= 4.857142857128634 \ 7 = 434 Mod 7 = 6
62
Mod Rules Chapter 4 - Visual Basic Schneider61 u The whole number remainder u If the numbers are single they are rounded first u If one or both of a and b are negative the result of the Mod will have the sign of “a” u num1 Mod 1 = 0 u num1 Mod num1 = 0 u Num Mod 0 Division by zero u Result of Mod should be less than num2
63
Mod Examples Chapter 4 - Visual Basic Schneider62 25 mod 7 18.7mod 3.2 -35 mod 4 -35 mod -4 35 mod -4 -18.7 mod 3.2 = 4 = 1 = -3 = 3 = -1 -18.7 mod -3.2 = -1 18.7 mod -3.2 = 1 3 mod 3 = 0 3 mod -3 = 0 -3 mod 3 = 0 -3 mod -3 = 0 3 mod 1 = 0 -3 mod 1 = 0
64
Operators Precedence Chapter 4 - Visual Basic Schneider63 u Precedence 1.() 2.^ 3.* 4./ 5.\ 6.mod 7.+ 8.-
65
2 Chapter 4 - Visual Basic Schneider64 Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a / b \ b End Sub Output: 1. 2.8 2. 0 3. 2 4. 4 5. Infinity /Illegal Division
66
2. Chapter 4 - Visual Basic Schneider65 Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a mod b \ b End Sub Output: 1. 2.8 2. 0 3. 2 4. 4 5. Infinity /Illegal Division
67
Chapter 4 - Visual Basic Schneider66 More Examples Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 Print a / b Print a \ b Print a Mod b End Sub Output: 2.8 2 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.