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

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Sub and Function Procedures
Sub Procedures and Functions. Procedures in VBA The main idea: encapsulate some code in its own procedure (There are two kinds: Sub Procedures and Functions)
1 CS 106, Winter 2009 Class 3, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1.
11 CS 106, Winter 2009 Class 20, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 14, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 19, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 11, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 8, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 2, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 4, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Chapter 4 General Procedures
VBA Modules, Functions, Variables, and Constants
Sub Programs To Solve a Problem, First Make It Simpler.
CS 201 Functions Debzani Deb.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Chapter 7: Sub and Function Procedures
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Chapter 51 Select Case block A decision-making structure that simplifies choosing among several actions. Avoids complex nested If constructs. If blocks.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Why to Create a Procedure
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Chapter 4: The Selection Structure
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Applications Development
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
JavaScript, Fourth Edition
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Controlling Program Flow with Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
CS0004: Introduction to Programming
Functions Chapter 6-Part 2.
CS 106 Computing Fundamentals II Chapter 33 “Conditional Statements”
Using Procedures and Exception Handling
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Visual Programming
Tonga Institute of Higher Education
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Presentation transcript:

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

2 Conditionals We’ve learned about several types of conditionals: – If/Else statements – Ifs with no Else – If/ElseIf statements – Select Case blocks – Nested conditionals We learned how to write Boolean expressions and analyze them using truth tables

3 Case Block example Assume position is a variable with a value between 1 and some number from 2 to 20 SelectCasepostion Case 1 txtOutcome.Text = “Win”‘several lines of code could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else‘action of last resort txtOutcome.Text = “Out of the money” End Select

4 Flowchart for Select Case Evaluate Selector In Value List 1? In Value List 2? In Value List n? Perform action of last resort Action 1 Action 2 Action n yes nono nono nono ~ ~ ~~

5 Conditional Controls We’ve learned about radio buttons and check boxes. These controls let us specify options to our program. Only one radio button in a group can be clicked. Any number of check boxes in a group can be clicked.

6 Assignment 4 The complete specification of Assignment 4 is on the website. Assignment 4 builds on Assignment 3, using conditionals. As before, you will – design the interface, – write the use cases and tests, – list the objects and their events, – name and describe the global variables you will use, – create flowcharts for the nontrivial events, and – write and test the program. Submit everything in your zipped project folder

7 Example for Assignment 4 We’ll look at an example that performs similar operations as Assignment 4. The project for this example is on the website in the IceCream2 zip file.

8 Ice Cream Store Enhancement We can still enter number of scoops We can choose the flavor (one flavor) We can choose toppings (as many as we want) At the end we get an itemized bill in a list box, with our total and the tip amount.

9 BREAK 10 minutes

10 General Procedures The main idea: encapsulate some code in its own procedure (Sub or Function) We’ve seen this in event procedures already 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

11 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

12 SubProcedures A subProcedure has a name, possible parameters, and a body of code. Example procedure definition: Sub DisplaySum( 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

13 Procedure Name Let’s consider the parts of the header: Sub DisplaySum( ByVal num1 as Double, ByVal num2 as Double) DisplaySum is the name of the procedure. We’ll start procedure names with capital letters and variable names with lower-case letters. The name is used when use (call) the procedure elsewhere in the program

14 Parameters Sub DisplaySum(ByVal num1 as Double, ByVal num2 as Double) The parameters are num1 and num2 A ByVal parameter is very similar to a local variable Its type is declared in the header (these are type Double) Its initial value is set in the procedure call Changes to ByVal parameters have no effect on variables elsewhere in the program; still, it is best as a programming practice not set them to new values inside the procedure

15 Procedure Body Sub DisplaySum( 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 Note the local variable sum The result of calling this procedure is to make some text appear in list box lstResult

16 Procedure Call A procedure is called from elsewhere in the program, for example from within some event procedure Here’s what it might look like: Dim aVar, bVar As Double aVar = 2 bVar = 3 DisplaySum( 4, 5 ) ‘parameter values are literal numbers DisplaySum( aVar, bVar – 2 ) ‘parameter values are expressions

17 What Happens in a Procedure Call The expressions for the parameter values are evaluated, and the 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

18 A Picture of the Control Flow Some program code Procedure call More program code Procedure code Set parameter values

19 Two Aspects of Procedures The Defintion: 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. We’ll see examples of code with procedures in our next class.