Download presentation
Presentation is loading. Please wait.
2
Chapter 4 (cont) Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
3
Well-designed programs... §consist of highly modular code §have procedures that pass data between them l data is passed to & from the calling procedure in the form of arguments l a procedure declares parameters to represent each argument that is sent to it §have other important features but our focus here is on procedures...
4
Relationship between arguments & parameters... §Where are they declared? l Arguments are declared in the calling procedure. l Parameters are declared in the procedure in the procedure header. §How must they correspond? l They must be in with respect to their placement in the procedure call (arguments) & procedure header (parameters)... perfect agreement
5
Perfect agreement means... §#arguments in the call = #parameters in the procedure header §the type of an argument = the type of its corresponding parameter
6
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
7
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)
8
What arguments & parameters look like... §Arguments may be: l constants - used to send values to a procedure l variables - used to send values to a procedure and/or to receive values back from a procedure l expressions - used to send values to a procedure §Parameters must be: l variables - declared by mini-declarations in the procedure header parameter list
9
What can be accomplished within a procedure?
10
To be more dignified... §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.
11
Local variables §Declare variables for use in your procedures for the same reason you would declare them in an event procedure: l to receive input, or l to receive an intermediate result §Ex. 4, p. 146 l local variable is rawDensity
12
Once you decide on the procedures you need... §Name your procedures appropriately l use descriptive names §You must determine l what input your procedure requires l what output your procedure requires §These input & output items become the parameters
13
Examples... §Let’s write our own... l p. 162 #54 - use two procedures: getinput (job, amount, tiprate) show output (job, amt, rate)
14
Assignment... §Lab work l VB projects - pp. 137+ 1, 2, 6 l Written work: p. 151: 4, 10, 12, 22, 26, 34 p. 171: 2-10 (even), 15, 18 §Homework Due Tuesday l VB projects: p. 176 - 24, 32 l Read all of Chapter 4, esp. Sec. 4.3 (functions)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.