Download presentation
Presentation is loading. Please wait.
Published byLeslie Sherman 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
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) 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 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.
43
Chapter 4 - Visual Basic Schneider42 Returning Value A function can receive many values Only one value can be directly returned
44
Chapter 4 - Visual Basic Schneider43 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
45
Chapter 4 - Visual Basic Schneider44 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
46
Chapter 4 - Visual Basic Schneider45 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
47
Chapter 4 - Visual Basic Schneider46 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
48
Chapter 4 - Visual Basic Schneider47 Last 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.