Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 - Visual Basic Schneider

Similar presentations


Presentation on theme: "Chapter 4 - Visual Basic Schneider"— Presentation transcript:

1 Chapter 4 - Visual Basic Schneider
General Procedures Chapter 4 - Visual Basic Schneider

2 Chapter 4 - Visual Basic Schneider
Outline & Objective Sub Procedures Procedure Parameters Function Procedures Modular Design Chapter 4 - Visual Basic Schneider

3 Chapter 4 - Visual Basic Schneider
What is a procedure? 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 Procedures make a program easier to develop, test, and correct Code written to perform one well-defined subtask is referred to as a Sub procedure or function A Sub procedure is a part of a program that performs one or more related tasks, has its own name, To distinguish them from event procedures, Sub procedures and functions are referred to as a General Procedures Sub procedures allow us to focus on the tasks and later on how to accomplish each task Chapter 4 - Visual Basic Schneider

4 Chapter 4 - Visual Basic Schneider
General Procedures In Visual Basic, there are two types of procedures: Sub Function Note: To distinguish procedures from event procedures, Sub and Function procedures are referred to as general procedures. As a rule, a Sub procedure should perform only one task, or several closely related tasks, and should be kept relatively small. Chapter 4 - Visual Basic Schneider

5 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 Chapter 4 - Visual Basic Schneider

6 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. Chapter 4 - Visual Basic Schneider

7 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 its parameters. Chapter 4 - Visual Basic Schneider

8 Chapter 4 - Visual Basic Schneider
Sub Procedure's Name The rules for naming Sub Procedures are the same as naming variables. In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names. Chapter 4 - Visual Basic Schneider

9 Syntax of a Sub Procedure
Private Sub ProcedureName ( ) statement(s) End Sub Chapter 4 - Visual Basic Schneider

10 Creating Visual Basic Sub Procedure:
Activate the code window Select Add Procedure from the Tools menu Type in the name of the Sub procedure Click on Private in the Scope box Press the Enter key or click the OK button Type the statements of the Sub procedure into the code window Chapter 4 - Visual Basic Schneider

11 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 softDrinks = Val(txtDrinks.Text) Chapter 4 - Visual Basic Schneider

12 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

13 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. Chapter 4 - Visual Basic Schneider

14 Chapter 4 - Visual Basic Schneider
Example of Arguments Call Triple(num) Example 5.4.1 Chapter 4 - Visual Basic Schneider

15 Chapter 4 - Visual Basic Schneider
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. Chapter 4 - Visual Basic Schneider

16 Chapter 4 - Visual Basic Schneider
Example of Parameters Private Sub Triple(num As Single) Chapter 4 - Visual Basic Schneider

17 Passing arguments to parameters
Call Triple(num) Private Sub Triple (num As Single) Argument Go over example 4.1.6 The second CALL statement uses different variable names for the arguments to show that using the same argument names is not necessary. Parameter Chapter 4 - Visual Basic Schneider

18 Passing Arguments to Parameters
Call Add (x, y ) Private Sub Add (num1 As Single, num2 As Single) Arguments Go over example 4.1.6 The second CALL statement uses different variable names for the arguments to show that using the same argument names is not necessary. Parameters Chapter 4 - Visual Basic Schneider

19 Chapter 4 - Visual Basic Schneider
Passing Arguments A Sub procedure receives the location of the arguments, and may use and modify the value of the arguments stored at that location. Two-way into and out of the Sub Procedure. Example 1.4.2 It provides a vehicle for passing values from a Sub procedure back to the place which the Sub procedure was called Different names may be used for an arguments and its corresponding parameter Only one memory location is involved Example 2.4.2 Chapter 4 - Visual Basic Schneider

20 Chapter 4 - Visual Basic Schneider
Passing Arguments Private Sub cmdDisplay_Click() Dim amt As Single amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub Chapter 4 - Visual Basic Schneider

21 Chapter 4 - Visual Basic Schneider
Sub Triple Private Sub Triple(num As Single) ' Triple a number picResults.Print num; num = 3 * num End Sub Chapter 4 - Visual Basic Schneider

22 Chapter 4 - Visual Basic Schneider
Passing Arguments Call Triple(amt) Private Sub Triple (num As Single) amt num Chapter 4 - Visual Basic Schneider

23 Passing Data - by Reference
amt amt amt 2 2 6 6 num num Chapter 4 - Visual Basic Schneider

24 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. Chapter 4 - Visual Basic Schneider

25 Chapter 4 - Visual Basic Schneider
Local Variables: A variable that is used only in a specific procedure (Sub or Function). The scope of the local variable is the Sub or Function in which that variable has been defined. When the same variable name appears in two different Sub procedure, Visual Basic gives the variables a separate identity and treats them as two different variables. Also each time a Sub procedure is called, all declared variables that are not parameters assume their default values. Example (the value of num is set to default value) Example (x is treated as different variable) Chapter 4 - Visual Basic Schneider

26 Chapter 4 - Visual Basic Schneider
Local Variables: Declared within a procedure definition Private to a procedure definition Variables in different procedures are totally independent Different procedures can have variables with the same names; however, each variable will have its own memory location Chapter 4 - Visual Basic Schneider

27 Advantages of Local Variables
Extremely useful for team programming Protects against accidental side effects (change of the value of the variable in one procedure that causes an error in another) Chapter 4 - Visual Basic Schneider

28 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 Chapter 4 - Visual Basic Schneider

29 Chapter 4 - Visual Basic Schneider
Sub Add Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub Chapter 4 - Visual Basic Schneider

30 Chapter 4 - Visual Basic Schneider
Form-Level Variables Form-level variables are visible to every procedure. Form-level variables appear at the top of the code window. Making a variable visible to every procedure in a form’s code without being passed. When a form-level variable is assigned a value by a procedure, it retains its value when the procedure is exited Chapter 4 - Visual Basic Schneider

31 How to create Form-Level Variables?
Activate the code window Click on the down arrow to the right of the object list box Click on General Click on Declaration in the procedure list box Type in Dim statements for form-level variables Example 4.2.6 Delete Dim statements in Declaration section of General and show the resut The sum of 2 and 3 is 5 num1 = 2 num2 = 3 Chapter 4 - Visual Basic Schneider

32 Chapter 4 - Visual Basic Schneider
Example ' In Declaration section of General Dim num1 As Single, num2 As Single Chapter 4 - Visual Basic Schneider

33 Chapter 4 - Visual Basic Schneider
Common Errors Passing incorrect data types. Not returning the result of the computation back to the calling program. The only way that a Sub procedure can return a value is through its argumnets Chapter 4 - Visual Basic Schneider

34 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

35 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

36 Chapter 4 - Visual Basic Schneider
What is a function? Similar to a Sub, performs a specific task Unlike a Sub, returns a single value to the calling program Chapter 4 - Visual Basic Schneider

37 Chapter 4 - Visual Basic Schneider
Types of Functions Standard functions (built-in) User-defined functions Chapter 4 - Visual Basic Schneider

38 User-Defined Function
A function returns a single value The value is returned by assigning a value to the name of the function The values of the parameters of a function should not be used for returning values from a function It returns a single value This value is returned in the function itself, not as an argument A function is called by using its name in an expression the arguments of a function should not be altered in the function If a procedure needs to return more than one value, a SUB procedure should be used instead. Chapter 4 - Visual Basic Schneider

39 Chapter 4 - Visual Basic Schneider
The Function Syntax Private Function FunctionName(parameter-list) As dataType Statement(s)…… ….. FunctionName = value End Function Note: value must be of the type dataType specified in the function declaration Chapter 4 - Visual Basic Schneider

40 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

41 Rules for Defining and Calling a Function
User-defined function must include a statement that assigns the function name a value. User-defined functions are called in the same way that built-in functions are called. A user-defined function may be called in an expression. Chapter 4 - Visual Basic Schneider

42 A function can receive many values
Returning Value A function can receive many values Only one value can be directly returned Chapter 4 - Visual Basic Schneider

43 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

44 Chapter 4 - Visual Basic Schneider
Common Errors Passing incorrect data types Not specifying the data type of the returned value Not assigning a value to the Function name inside the Function definition Misspelling of the Function name Chapter 4 - Visual Basic Schneider

45 Chapter 4 - Visual Basic Schneider
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) Private Function Area(r As Single) As Single Area = * r ^ 2 End Function Chapter 4 - Visual Basic Schneider

46 Chapter 4 - Visual Basic Schneider
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 Chapter 4 - Visual Basic Schneider

47 Chapter 4 - Visual Basic Schneider
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 num = 3 Triple = num * x End Function Output: 15 5 Chapter 4 - Visual Basic Schneider


Download ppt "Chapter 4 - Visual Basic Schneider"

Similar presentations


Ads by Google