Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,"— Presentation transcript:

1 1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.educbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.eduherb@cs.pdx.edu

2 2

3 3 Individual Vote Impact Chart (from NY Times) White number = registered voters Black number = electoral votes

4 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 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 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 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 sum Dim sum as Double sum = num1 + num2 lstResult.Items.Add(“The sum of “ & num1 &“ and “ & _ num2 &“ is “ & sum &“.” End Sub

8 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 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 sum Dim sum as Double sum = num1 + num2 Return(sum) End Sub

10 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 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 Double aVar = 2 bVar = 3 DisplaySum(aVar, bVar – 2) cVar = ComputeSum(aVar, bVar) ‘silly example since we could just add

12 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 13 A Picture of the Control Flow for a Procedure Call Some program code Procedure call More program code Procedure code Set parameter values

14 14 Let’s look at some examples.

15 15 BREAK 10 minutes


Download ppt "1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,"

Similar presentations


Ads by Google