1 / 54

CS 4 Intro to Programming using Visual Basic

Chapter 4 - VB 2005 by Schneider. 2. Chapter 4 - General Procedures. 4.1 Sub Procedures, Part I4.2 Sub Procedures, Part II4.3 Function Procedures4.4 Modular Design. Chapter 4 - VB 2005 by Schneider. 3. 4.1 Sub Procedures, Part I. Sub ProceduresVariables and Expressions as ArgumentsCalling Other

maurizio
Download Presentation

CS 4 Intro to Programming using Visual Basic

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. CS 4 Intro to Programming using Visual Basic Sub Procedures Patchrawat Uthaisombut University of Pittsburgh 1

    2. Chapter 4 - VB 2005 by Schneider 2 Chapter 4 - General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design

    3. Chapter 4 - VB 2005 by Schneider 3 4.1 Sub Procedures, Part I Sub Procedures Variables and Expressions as Arguments Calling Other Sub Procedures

    4. Chapter 4 - VB 2005 by Schneider 4 Devices for modularity Visual Basic has two devices for breaking problems into smaller pieces: Sub procedures Function procedures

    5. Chapter 4 - VB 2005 by Schneider 5 Sub Procedures Perform one or more related tasks General syntax Sub ProcedureName() statements End Sub

    6. Chapter 4 - VB 2005 by Schneider 6 Calling a Sub procedure The statement that invokes a Sub procedure is also referred to as a call statement. A call statement looks like this: ProcedureName()

    7. Uthaisombut & Schneider 7 Naming Sub procedures The rules for naming Sub procedures are the same as the rules for naming variables. Camel casing: lower case except the first letters of subsequent words computeAverageGrade( ) The name should describe the task it performs.

    8. Chapter 4 - VB 2005 by Schneider 8 Example lstBox.Items.Clear()

    9. Chapter 4 - VB 2005 by Schneider 9 Passing Values You can send values to a Sub procedure Sum(2, 3) Sub Sum(ByVal num1 As Double, ByVal num2 As Double) lstBox.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & (num1 + num2) & "." End Sub In the Sum Sub procedure, 2 will be stored in num1 and 3 will be stored in num2

    10. Chapter 4 - VB 2005 by Schneider 10 Arguments and Parameters Sum(2, 3) Sub Sum(ByVal num1 As Double, ByVal num2 As Double)

    11. Chapter 4 - VB 2005 by Schneider 11 Several Calling Statements ExplainPurpose() Sum(2, 3) Sum(4, 6) Sum(7, 8) Output: Program displays a sentence identifying a sum. The sum of 2 and 3 is 5. The sum of 4 and 6 is 10 The sum of 7 and 8 is 15.

    12. Chapter 4 - VB 2005 by Schneider 12 Passing Strings and Numbers Demo("CA", 34) Sub Demo(ByVal state As String, ByVal pop As Double) txtBox,Text = state & " has population " & pop & _ " million." End Sub Note: The statement Demo(34, "CA") would not be valid. The types of the arguments must be in the same order as the types of the parameters.

    13. Chapter 4 - VB 2005 by Schneider 13 Variables and Expressions as Arguments Dim s As String = "CA" Dim p As Double = 17 Demo(s, 2 * p) Sub Demo(ByVal state As String, ByVal pop As Double) txtBox.Text = state & " has population " & pop & _ " million." End Sub Note: The variable names in the arguments need not match the parameter names. For instance, s versus state..

    14. Chapter 4 - VB 2005 by Schneider 14 Calling A Sub procedure can call another Sub procedure. Private Sub btnAdd_Click(...) Handles btnAdd.Click Sum(2, 3) End Sub Sub Sum(ByVal num1 As Double, ByVal num2 As Double) DisplayPurpose() lstBox.Items.Add("The sum of " & num1 & " and " _ & num2 & " is " & (num1 + num2) & "." End Sub

    15. Example …CS0004-utp/examples/utp-CallingSub P. Uthaisombut 15

    16. Chapter 4 - VB 2005 by Schneider 16 4.2 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging

    17. Chapter 4 - VB 2005 by Schneider 17 ByVal and ByRef Parameters in Sub procedure headers are proceeded by ByVal or ByRef ByVal stands for By Value ByRef stands for By Reference

    18. Chapter 4 - VB 2005 by Schneider 18 Passing by Value When a variable argument is passed to a ByVal parameter, just the value of the argument is passed. After the Sub procedure terminates, the variable has its original value.

    19. Chapter 4 - VB 2005 by Schneider 19 Example

    20. Chapter 4 - VB 2005 by Schneider 20 Passing by Reference When a variable argument is passed to a ByRef parameter, the parameter is given the same memory location as the argument. After the Sub procedure terminates, the variable has the value of the parameter.

    21. Chapter 4 - VB 2005 by Schneider 21 Same Example: n num

    22. Chapter 4 - VB 2005 by Schneider 22 Local Variable A variable declared inside a Sub procedure with a Dim statement Space reserved in memory for that variable until the End Sub – then the variable ceases to exist

    23. Chapter 4 - VB 2005 by Schneider 23 Local Variables Private Sub btnOne_Click(...) Handles btnOne_Click Dim num As Integer = 4 SetFive() txtBox.Text = CStr(num) End Sub Sub SetFive() Dim num As Integer = 5 End Sub Output: 4

    24. Chapter 4 - VB 2005 by Schneider 24 Class-Level Variables Visible to every procedure in a form’s code without being passed Dim statements for class-level variables are placed Outside all procedures At the top of the program region

    25. Chapter 4 - VB 2005 by Schneider 25 Class-Level Variables Dim num As Integer = 4 Private Sub btnOne_Click(...) Handles btnOne_Click txtBox.Text = CStr(num) SetFive() txtBox.Text &= CStr(num) End Sub Sub SetFive() num = 5 End Sub Output: 45

    26. Chapter 4 - VB 2005 by Schneider 26 Scope The scope of a variable is the portion of the program that can refer to it.

    27. Chapter 4 - VB 2005 by Schneider 27 Scope Class-level variables have class-level scope and are available to all procedures in the class. Variables declared inside a procedure have local scope and are only available to the procedure in which they are declared.

    28. Chapter 4 - VB 2005 by Schneider 28 Debugging Programs with Sub procedures are easier to debug Each Sub procedure can be checked individually before being placed into the program

    29. Chapter 4 - VB 2005 by Schneider 29 4.3 Function Procedures User-Defined Functions Having Several Parameters Comparing Function Procedures with Sub Procedures Collapsing a Procedure with a Region Directive

    30. Chapter 4 - VB 2005 by Schneider 30 Some Built-In Functions

    31. Chapter 4 - VB 2005 by Schneider 31 Function Procedures Function procedures (aka user-defined functions) always return one value Syntax: Function FunctionName(ByVal var1 As Type1, _ ByVal var2 As Type2, _ …) As dataType statement1 statement2 … Return expression End Function

    32. Chapter 4 - VB 2005 by Schneider 32 Example: Form ByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receivesByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receives

    33. Chapter 4 - VB 2005 by Schneider 33 Example: Code Private Sub btnDetermine_Click(...) _ Handles btnDetermine.Click Dim name As String name = txtFullName.Text txtFirstName.Text = FirstName(name) End Sub Function FirstName(ByVal name As String) As String Dim firstSpace As Integer firstSpace = name.IndexOf(" ") Return name.Substring(0, firstSpace) End Function The "As String" at the end of the line which begins "Function First Name" is the data type of the value being returned from this function The "As String" at the end of the line which begins "Function First Name" is the data type of the value being returned from this function

    34. Chapter 4 - VB 2005 by Schneider 34 Example: Form ByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receivesByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receives

    35. Chapter 4 - VB 2005 by Schneider 35 Example: Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim a, b As Double a = CDbl(txtSideOne.Text) b = CDbl(txtSideTwo.Text) txtHyp.Text = CStr(Hypotenuse(a, b)) End Sub Function Hypotenuse(ByVal a As Double, _ ByVal b As Double) As Double Return Math.Sqrt(a ^ 2 + b ^ 2) End Function ByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receivesByVal is used in function parameters so the function will not inadvertently return more than one value Value in return statement is sent back to the place where the function was called – the function call "becomes" that value – which must then be stored, or printed, or used in some way by the calling routine. There is a one to one relationship between the arguments being passed to the function and the parameters that the function receives

    36. Chapter 4 - VB 2005 by Schneider 36 User-Defined Function Having No Parameters Private Sub btnStart_Click(...) Handles btnStart.Click txtBox.Text = Saying() End Sub Function Saying() As String Dim strVar As String strVar = InputBox("What is your favorite saying?") Return strVar End Function

    37. Chapter 4 - VB 2005 by Schneider 37 Comparing Function Procedures with Sub Procedures Subs are accessed using a call statement procedureName(…) Functions are called where you would expect to find a literal or expression result = functionName(…) lstBox.Items.Add (functionName(…))

    38. Chapter 4 - VB 2005 by Schneider 38 Functions vs. Procedures Both can perform similar tasks Both can call other subs and functions Use a function when you want to return one and only one value

    39. Chapter 4 - VB 2005 by Schneider 39 Collapsing a Procedure with a Region Directive A procedure can be collapsed behind a captioned rectangle This task is carried out with a Region directive. To specify a region, precede the code to be collapsed with a line of the form #Region "Text to be displayed in the box." and follow the code with the line #End Region A procedure, or for that matter any block of code, can be collapsed behind a captioned rectangle just like the Windows Form Designer-generated code. This task is carried out with a so-called Region directive. To specify a region, precede the code to be collapsed with a line of the form #Region "Text to be displayed in the box." and follow the code with the line #End Region A tiny box holding a minus sign will appear to the left of the #Region line. To collapse the code, click on the minus sign. The code will be hidden behind a rectangle captionedA procedure, or for that matter any block of code, can be collapsed behind a captioned rectangle just like the Windows Form Designer-generated code. This task is carried out with a so-called Region directive. To specify a region, precede the code to be collapsed with a line of the form #Region "Text to be displayed in the box." and follow the code with the line #End Region A tiny box holding a minus sign will appear to the left of the #Region line. To collapse the code, click on the minus sign. The code will be hidden behind a rectangle captioned

    40. Chapter 4 - VB 2005 by Schneider 40 Region Directives

    41. Chapter 4 - VB 2005 by Schneider 41 Collapsed Regions

    42. Chapter 4 - VB 2005 by Schneider 42 4.4 Modular Design Top-Down Design Structured Programming Advantages of Structured Programming

    43. Chapter 4 - VB 2005 by Schneider 43 Design Terminology Large programs can be broken down into smaller problems "divide-and-conquer" approach called "stepwise refinement" Stepwise refinement is part of top-down design methodology

    44. Chapter 4 - VB 2005 by Schneider 44 Top-Down Design General problems are at the top of the design Specific tasks are near the end of the design Top-down design and structured programming are techniques to enhance programmers' productivity

    45. Chapter 4 - VB 2005 by Schneider 45 Top-Down Design Criteria The design should be easily readable and emphasize small module size. Modules proceed from general to specific as you read down the chart. The modules, as much as possible, should be single minded. That is, they should only perform a single well-defined task. Modules should be as independent of each other as possible, and any relationships among modules should be specified.

    46. Chapter 4 - VB 2005 by Schneider 46 Top-Level Design HIPO Chart

    47. Chapter 4 - VB 2005 by Schneider 47 Detailed HIPO Chart

    48. Chapter 4 - VB 2005 by Schneider 48 Structured Programming Control structures in structured programming: Sequences: Statements are executed one after another. Decisions: One of two blocks of program code is executed based on a test for some condition. Loops (iteration): One or more statements are executed repeatedly as long as a specified condition is true.

    49. Chapter 4 - VB 2005 by Schneider 49 Advantages of Structured Programming Goal to create correct programs that are easier to write understand modify "GOTO –less" programming

    50. Chapter 4 - VB 2005 by Schneider 50 Comparison of Flow Charts

    51. Chapter 4 - VB 2005 by Schneider 51 Easy to Write Allows programmer to first focus on the big picture and take care of the details later Several programmers can work on the same program at the same time Code that can be used in many programs is said to be reusable

    52. Chapter 4 - VB 2005 by Schneider 52 Easy to Debug Procedures can be checked individually A driver program can be set up to test modules individually before the complete program is ready Using a driver program to test modules (or stubs) is known as stub testing

    53. Chapter 4 - VB 2005 by Schneider 53 Easy to Understand Interconnections of the procedures reveal the modular design of the program. The meaningful procedure names, along with relevant comments, identify the tasks performed by the modules. The meaningful variable names help the programmer to recall the purpose of each variable.

    54. Chapter 4 - VB 2005 by Schneider 54 Easy to Change Because a structured program is self-documenting, it can easily be deciphered by another programmer.

    55. Chapter 4 - VB 2005 by Schneider 55 Object-Oriented Programming an encapsulation of data and code that operates on the data objects have properties, respond to methods, and raise events.

More Related