1 / 68

Chapter 4

Chapter 4. General Procedures. Outline & Objective. Sub Procedures Procedure Parameters Function Procedures. 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

Download Presentation

Chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. Sub Procedures Properties: can be called (invoked) by using call subProcedureName() passing data called arguments Chapter 4 - Visual Basic Schneider

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

  7. Syntax of a Sub Procedure Private Sub ProcedureName ( ) statement(s) End Sub Procedure Name Example Statements

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

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

  10. Example

  11. Example

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

  13. Passing arguments to parameters • Arguments : Variables or expressions placed in parentheses in a Call statement. Arguments Chapter 4 - Visual Basic Schneider 13

  14. Passing arguments to parameters • Parameters : Variables appearing in the sub procedure definition are called paramanters. Parameters 14

  15. Passing Arguments to Parameters Call Add (x, y ) Private Sub Add ( num1 As Single, num2 As Single) When the Call Statement is executed , each value of the arguments is assigned to it’s corresponding parameter variable the value of num1 is assigned to x and the value of num2 is assigned to y Arguments Parameters 15

  16. Passing arguments to parameters Call Triple(num) Private Sub Triple (num As Single) Argument Parameter Chapter 4 - Visual Basic Schneider

  17. 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 cmdCompute_Click() Dim num As Single num = Val(InputBox("Enter a number:")) Call Triple(num) End Sub cmdCompute Triple num par/num Private Sub Triple(par As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * par End Sub Chapter 4 - Visual Basic Schneider

  18. Example

  19. Example cmdDisplay Triple amt num

  20. Passing by value • Sometime you want to pass a variable to a sub procedure, but you want to ensure that the variable will retain its original value after the sub procedure terminates. • Such variable is said to be passed by value

  21. Passing by value • There are two ways to pass the variable by value 1. In the call statements, enclose the variable in an extra pairs of parenthesis cmdDisplay Triple amt num

  22. 2. In the private Sub statement, precede the corresponding parameter with the word ByVal cmdDisplay Triple amt num

  23. Passing by value • When the variable is passed by value, the variable will retain its original value cmdDisplay Triple amt num

  24. 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 cmdCall mysub a x b y

  25. 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 cmdCall mysub a x b y

  26. 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 cmdCall mysub a x b y

  27. 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 10 15 cmdCall mysub a b x y

  28. 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 cmdCall mysub a y b x

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

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

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

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

  33. Example cmdDisplay pr1 x y x y The variables x and y are local for the procedure cmdDisplay_Click The variables x and y are local for the procedure pr1

  34. Any change of the value of parameters (x and y) will affect only on the value of its corresponding arguments (a and b) in the caller procedure cmdCall mysub a x b y x y z Local variables

  35. Form-Level Variables Form-level variables are visible to every procedure. Form-level variables appear at the top of the code window. Chapter 4 - Visual Basic Schneider

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

  37. The variables x and y are Form-Level variables (Global) cmdCall mysub X Global X Global y Global y Global Note: The values of Form-level variables are visible to all procedures in the program

  38. y x x y • If the procedure has a local variables with the same name of the form-level variables, the procedure uses its local variable instead of the global variable Form_level Variables 0 7 6 0 Local 5 0 The procedure uses the global variable if it has no local variable 8 0 Local

  39. Examples 23 23 23 23 23 23

  40. Form_load • In some situation we want to assign the value immediately to the Form-level variable • Visual basic has a special event procedure called Form_Load that is executed as soon as the program is run.

  41. Example Command1 pro1 X Global X Global y Global y Global / b x a

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

  43. Types of Functions • Standard functions (built-in) • VBasic has many bulit-in functions (Predefined functions). • E.g. (Left, Right, Len, Sqr, Round, …) • User-defined functions: In addition to using bulit-in functions, we can define functions of our own. These new function are called function procedures or user-defined functions

  44. Built-in Functions • Print Len(“Hello”)  5 • Print Left(“Hello”,3)  “Hel” • Print Sqr(9)  3 • For example, the function Len returns the number of characters in the string. It has One parameter of type String and returns only one value of type Single

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

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

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

  48. Example of a Function The function Max has 2 parameters of type single What is the data type of the returned value?

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

  50. Another Example Private Sub cmdDisplay_Click() Dim num As Single num = 5 picOutput.Print Triple (num) picOutput.Print num End Sub Private FunctionTriple(x As Single) As Single Dim num As Single num = 3 Triple= num * x End Function Output: 15 5 Chapter 4 - Visual Basic Schneider

More Related