Download presentation
Presentation is loading. Please wait.
Published byJasmin Brooks Modified over 9 years ago
2
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures
3
Chapter 4 - Visual Basic Schneider2 Outline & Objective Creating Visual Basic Sub Procedures Creating programmer-defined Function Procedures Parameter Passing Modularizing in Programming Languages
4
Chapter 4 - Visual Basic Schneider3 What is Modularization Breaking the program into smaller parts A Sub procedure or Function performs a well-defined task Easier to test, debug and correct
5
Chapter 4 - Visual Basic Schneider4 Modularizing Programs in Visual Basic In Visual Basic, there are three types of procedures: Event Sub Function Note: To distinguish procedures from event procedures, Sub and Functions are referred to as general procedures.
6
Chapter 4 - Visual Basic Schneider5 Passing Arguments to Subs: 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.
7
Chapter 4 - Visual Basic Schneider6 Sub Procedures Properties: may be called may be passed data called arguments may return values to the calling program may change the data stored in a received variable
8
Chapter 4 - Visual Basic Schneider7 Components of Sub Procedure: name: used to identify the Sub procedure parameters: a Sub procedure accepts values from the caller through its parameters; it may also send values back to the caller through it’s parameters.
9
Chapter 4 - Visual Basic Schneider8 Sub Procedure's Name 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: Activate a code window Select Add Procedure from the Tools menu Type in the name of the Sub procedure Click on Private in Scope frame Press the Enter key or click the OK button Type the statements of the Sub procedure into this window
12
Chapter 4 - Visual Basic Schneider11 Example of Call to a Sub Procedure: Private Sub cmdCompute_Click() Dim sngNum As Single sngNum = Val(InputBox("Enter a number:")) Call Triple(sngNum) 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 Arguments : Variables or expressions placed in parentheses in a Call statement. 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 Parameters Variables placed in parentheses after a Sub Procedure's name. When the procedure is called, the values of the corresponding arguments are placed in the parameters.
16
Chapter 4 - Visual Basic Schneider15 Example of Parameters Private Sub Triple(num As Single)
17
Chapter 4 - Visual Basic Schneider16 Passing arguments to parameters Call Triple(num ) Private Sub Triple (num As Single) Argument Parameter
18
Chapter 4 - Visual Basic Schneider17 Passing Arguments to Parameters Call Add (x, y ) Private Sub Add ( num1 As Single, num2 As Single) Parameters Arguments
19
Chapter 4 - Visual Basic Schneider18 Passing Arguments The Sub Procedure receives the location of the arguments, the Sub Procedure may use and modify the value of the arguments.
20
Chapter 4 - Visual Basic Schneider19 Important Rules for Passing Arguments to a Sub The number of arguments and parameters must match. The data type of each argument must match its corresponding parameter.
21
Chapter 4 - Visual Basic Schneider20 Review Visual Basic has three types of procedures: Event Sub Function Each Sub procedure performs a distinct task. The Call statement causes a Sub procedure to be executed.
22
Chapter 4 - Visual Basic Schneider21 Review Values can be passed between the calling program and Sub by passing arguments. The number and type of arguments in the calling program and Sub must match.
23
Chapter 4 - Visual Basic Schneider22 Review Variables that are used in a particular Sub are local variables. Values that are assigned to them are not returned to the calling module.
24
Chapter 4 - Visual Basic Schneider23 Common Errors Passing incorrect data types. Not returning the result of the computation back to the calling program.
25
Chapter 4 - Visual Basic Schneider24 What is a function? A function designed to perform a specific task also. A function designed to return a single value to the calling program.
26
Chapter 4 - Visual Basic Schneider25 Types of Functions Standard functions (built-in) programmer-defined functions
27
Chapter 4 - Visual Basic Schneider26 programmer-defined Function A function designed to return a single value. The value is returned in the function itself. The arguments of a function should not be changed in the function body.
28
Chapter 4 - Visual Basic Schneider27 The Function Syntax Private Function FunctionName (parameter-list) As datatype Statement(s)…… ….. FunctionName = …….. End Function
29
Chapter 4 - Visual Basic Schneider28 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
30
Chapter 4 - Visual Basic Schneider29 Rule for Defining and Calling a Function programmer-defined function must include a statement that assigns the function name a value. programmer-defined functions are called in the same way that built-in functions are called. A programmer-defined function may be called in an expression.
31
Chapter 4 - Visual Basic Schneider30 Common Errors Passing incorrect data types Not specifying the data type of the returned value Forgetting the data type of a function's parameter
32
Chapter 4 - Visual Basic Schneider31 Common Errors Not assigning a value to the function name inside the function definition Misspelling of the Function name Wrong invoking of the function in an expression
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.