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

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

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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

Procedures (User-defined)

Page 2: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

Well-designed programs...consist of highly modular codehave 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...

Page 3: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

Relationship between arguments & parameters...Where are they declared?

Arguments are 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)...

perfect agreement

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

Perfect agreement means...

#arguments in the call = #parameters in the procedure header

the type of an argument = the type of its corresponding parameter

Page 5: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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

Page 6: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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)

Page 7: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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

Page 8: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

What can be accomplished within a procedure?

Page 9: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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.

Page 10: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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

Page 11: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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

Page 12: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

Examples...

Let’s write our own... p. 162 #54 - use two procedures:

• getinput (job, amount, tiprate)• show output (job, amt, rate)

Page 13: Chapter 4 (cont) Sec. 4.1, 4.2, 4.4

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)