1 / 13

Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

Chapter 4 (cont) Sec. 4.1, 4.2, 4.4. Procedures (User-defined). Well-designed programs. consist of highly modular code have procedures that pass data between them data is passed to & from the calling procedure in the form of arguments

hall-robles
Télécharger la présentation

Chapter 4 (cont) Sec. 4.1, 4.2, 4.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 (cont)Sec. 4.1, 4.2, 4.4 Procedures (User-defined)

  2. Well-designed programs... • consist of highly modular code • have procedures that pass data between them • data is passed to & from the calling procedure in the form of arguments • a procedure declares parameters to represent each argument that is sent to it • have other important features but our focus here is on procedures...

  3. Relationship between arguments & parameters... • Where are they declared? • Argumentsare declared in the calling procedure. • Parameters are declared in the procedure in the procedure header. • How must they correspond? • They must be in with respect to their placement in the procedure call (arguments) & procedure header (parameters)... perfectagreement

  4. Perfect agreement means... • #arguments in the call =#parameters in the procedure header • the type of an argument= the type of its corresponding parameter

  5. Passing values back from procedures OUTPUT parameters • If you want a procedure to change the value of an argument, you must use a variable for that argument. • When this happens, we say that the corresponding parameter is serving as an output parameter. • Often a parameter is used for both input & output

  6. Passing by value • Sometimes we want to send a variable argument to a procedure, but we want to ensure that the variable will retain its original value after the procedure has terminated. • In the parameter list of the procedure: Private Sub Triple (ByVal num As Single)

  7. What arguments & parameters look like... • Arguments may be: • constants - used to send values to a procedure • variables -used to send values to a procedure and/or to receive values back from a procedure • expressions - used to send values to a procedure • Parameters must be: • variables - declared by mini-declarations in the procedure header parameter list

  8. What can be accomplished within a procedure? Absolutely anything!!!

  9. To be more dignified... ahem • From now on, your event procedures should be designed in a modular fashion, consisting of calls to procedures that you create & define. • You may use any Visual Basic instructions you know within a procedure that you write.

  10. Local variables • Declare variables for use in your procedures for the same reason you would declare them in an event procedure: • to receive input, or • to receive an intermediate result • Ex. 4, p. 146 • local variable is rawDensity

  11. Once you decide on the procedures you need... • Name your procedures appropriately • use descriptive names • You must determine • what input your procedure requires • what output your procedure requires • These input & output items become the parameters

  12. Examples... • Let’s write our own... • p. 162 #54 - use two procedures: • getinput (job, amount, tiprate) • show output (job, amt, rate)

  13. Assignment... • Lab work • VB projects - pp. 137+ 1, 2, 6 • Written work: • p. 151: 4, 10, 12, 22, 26, 34 • p. 171: 2-10 (even), 15, 18 • Homework Due Tuesday • VB projects: p. 176 - 24, 32 • Read all of Chapter 4, esp. Sec. 4.3 (functions)

More Related