150 likes | 264 Vues
This presentation, part of the CS 106 course, explores the concept of procedures in programming. It discusses how to tackle complex problems by breaking them down into manageable parts, leading to better code organization through encapsulation. The slides cover the definition and purpose of subprocedures and function procedures, explaining how to create and call them effectively. Additionally, the presentation includes examples to illustrate these concepts and outlines the control flow during procedure calls in programming.
E N D
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
Individual Vote Impact Chart (from NY Times) White number = registered voters Black number = electoral votes
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
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
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.
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
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
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
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
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
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)
A Picture of the Control Flowfor a Procedure Call Some program code Set parameter values Procedure call Procedure code More program code
BREAK 10 minutes