Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)

Similar presentations


Presentation on theme: "Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)"— Presentation transcript:

1 Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)

2 Modular, Top-Down or Structured Program Design §Break the problem to be solved into discrete tasks: Ex:Read input Perform calculations Display output §Create a user-defined (general) procedure (or function) for each of the tasks.

3 Frequently asked questions about procedures in Visual Basic programs (I) §Don’t we already have procedures? l Yes: we have event procedures that are built into Visual Basic to respond to users’ interactions with the program; but we’re talking about something different here. §Where do we put the procedures we’re learning about here? l We call them from within event procedures l We create them with the Add Procedure tool

4 Why use procedures in Visual Basic? §Event procedures that have a lot of work to perform can become complex and difficult to understand & debug §From Section 4.4, (p. 189+): Large problems usually require large programs. §Stepwise refinement is the practice of breaking a complex problem into smaller subproblems & writing procedures to solve each subproblem

5 How to use procedures in Visual Basic §First, design the GUI for the program. * the driving force for GUI design is what will produce the nicest, friendliest user interface §Next, identify the event procedures that arise from the GUI. §Now, treat each event procedure as a big problem that can be broken down into smaller ones (subproblems).

6 Advantages of Top-down, Modular or Structured Design §Easier to write l can focus on one piece of the problem in the writing (algorithm development) phase in the testing phase l can share the work with others who work independently on different pieces of the problem l can repeatedly use the same piece of code ctd

7 Advantages of Top-down, Modular or Structured Design §Easier to debug l A module can be tested & debugged by itself, with no other piece of the program accomplished through the use of driver programs l A program can be tested & debugged with the introduction of each new module stub programming is used to stand in for missing procedures ctd

8 Advantages of Top-down, Modular or Structured Design §Easier to understand l a set of smaller subprograms is more manageable than one monolithic mass of code - both for the programmer & for others looking at the code §Easier to maintain l a set of smaller subprograms is more manageable than one monolithic mass of code - both for the original programmer & later ones

9 Components of Structured Programs §Modules (Procedures) l Sequences l Decisions l Iterations

10 Frequently asked questions about procedures in Visual Basic programs (II) §How do I get started? l design the form (GUI) all objects have been created & named l identify the work to be performed by each event procedure use general descriptions, not precise code l focus on one event procedure at a time ctd

11 Frequently asked questions about procedures in Visual Basic programs (III) §How do I create a procedure? l Write an event procedure with a call to a procedure. l With the code window active, select Add Procedure from the Tools menu l Type in the name of your procedure l Click on the circles next to Sub & Private l Click on OK

12 Procedure syntax - the Procedure Header Private Sub procedure_name (parameter_list) You choose this May be empty () or consist of several input & output parameters – we’ll see… End Sub

13 Procedure syntax - relating the Procedure Call & Header §From event procedure: Call mysub (argument_list) §Procedure header: Private Sub mysub (parameter_list)

14 Argument & parameter lists §Enable data values to be passed to and from a procedure §# arguments = # parameters §types of arguments must match types of parameters

15 Example: A procedure with no parameters (and, therefore, no arguments): l Within event procedure: Call MySub l In procedure definition: Private Sub Mysub () l Ex. 1, p. 143

16 First example of a procedure with parameters Call Firstsub (15,”abc”) ……. Private Sub Firstsub (a As Single, chstr As String) §This example involves INPUT parameters l their purpose is to supply values to procedure The arguments here are constants

17 More about INPUT parameters §Their corresponding arguments may be either constants or variables, or expressions involving constants and/or variables. Ex. 2, p. 144 - input parameters Ex. 3, p. 145 - input parameters/ several calls See page 147 Input parameters do not change value within the procedure


Download ppt "Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)"

Similar presentations


Ads by Google