130 likes | 215 Views
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
E N D
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 • a procedure declares parameters to represent each argument that is sent to it • have other important features but our focus here is on procedures...
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
Perfect agreement means... • #arguments in the call =#parameters in the procedure header • the type of an argument= the type of its corresponding parameter
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
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)
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
What can be accomplished within a procedure? Absolutely anything!!!
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.
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
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
Examples... • Let’s write our own... • p. 162 #54 - use two procedures: • getinput (job, amount, tiprate) • show output (job, amt, rate)
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)