1 cs 106, winter 2009 class 11, section 4 slides by: dr. cynthia a. brown,...

15
1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, [email protected] Instructor section 4: Dr. Herbert G. Mayer, [email protected]

Post on 19-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

1

CS 106, Winter 2009Class 11, Section 4

Slides by: Dr. Cynthia A. Brown, [email protected] section 4: Dr. Herbert G. Mayer, [email protected]

2

3

Individual Vote Impact Chart (from NY Times)

White number = registered votersBlack number = electoral votes

4

Problem-Solving Strategy

• Break a large, complex problem into smaller, more manageable parts

• Master a particular task that comes up repeatedly so you don’t have to think about how it works each time it occurs

5

General Procedures

• The main idea: encapsulate some code in its own procedure (Sub or Function)

• Why create our own procedures?– If we are repeatedly doing the same task, we can

write the code in one place and then call the procedure repeatedly

– If a procedure is too long and complex, we can break it into understandable parts

6

Two Aspects of Procedures

• The Definition: a separate piece of code where the procedure is defined, as we do with event procedures

• The Procedure Call: A piece of code that invokes a procedure

• Event procedures are invoked when the event happens.

7

Sub Procedures

• A subprocedure definition has a name, possibleformalparameters, and a body of code.

• Example procedure definition:

Private SubDisplaySum(ByVal num1 as Double, ByVal num2 as Double)‘Display two numbers and their sumDim sum as Double

sum = num1 + num2lstResult.Items.Add(“The sum of “ & num1 &“ and “ & _

num2 &“ is “ & sum &“.”End Sub

8

Function Procedures

• Function procedures have one extra element: they return a value

• We’ve seen examples of functions built into VB: for example, CDbl or FormatCurrency

• A function can have parameters• A function returns a result that has a

particular data type: Double for CDbl, String for FormatCurrency

9

Function Procedures

• Afunction procedure definition has a name, possibleformalparameters, a type, and a body of code.

• Example procedure definition:

Private FunctionComputeSum(ByVal num1 as Double, ByVal num2 as Double) as Double

‘Add two numbers and return their sumDim sum as Double

sum = num1 + num2Return(sum)End Sub

10

Note on Names

• Our convention is to name procedures starting with a capital letter

• I like to use a verb in the name, since a procedure carries out an action or computes a value

11

Procedure Call

• A procedure is called from elsewhere in the program, for example from within some event procedure

• It uses the procedure name and actual parameters (the book calls actual parameters arguments)

• Here’s what it might look like:DimaVar, bVar, cVarAs DoubleaVar = 2bVar = 3DisplaySum(aVar, bVar – 2)cVar = ComputeSum(aVar, bVar) ‘silly example since we could just add

12

What Happens in a Procedure Call

• The expressions for the actual parameter values are evaluated, and the formal parameters are set to those values

• The Dim statements for the procedure are used to create any local variables

• The code for the procedure is executed• Control returns to the line after the procedure

call (sub procedure), or to the line of the call with the returned value (function)

13

A Picture of the Control Flowfor a Procedure Call

Some program code

Procedure call

More program code

Procedure code

Set parameter values

14

Let’s look at some examples.

15

BREAK

10 minutes