1 / 15

CS 106, Winter 2009 Class 11, Section 4

CS 106, Winter 2009 Class 11, Section 4. Slides by: Dr. Cynthia A. Brown, cbrown@cs.pdx.edu Instructor section 4: Dr. Herbert G. Mayer, herb@cs.pdx.edu. 1. Individual Vote Impact Chart (from NY Times). White number = registered voters Black number = electoral votes.

nedaa
Download Presentation

CS 106, Winter 2009 Class 11, Section 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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

  4. 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

  5. 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.

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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)

  12. A Picture of the Control Flowfor a Procedure Call Some program code Set parameter values Procedure call Procedure code More program code

  13. Let’s look at some examples.

  14. BREAK 10 minutes

More Related