Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6 Procedures and Functions.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Advertisements

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Unemployment: Search and Efficiency Wages.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Multiple Forms, Standard Modules, And Menus
Lists, Loops, Validation, and More
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
BASIC SKILLS AND TOOLS USING ACCESS
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
1.
Solve Multi-step Equations
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 10 Arrays and Tile Mapping Starting Out with Games & Graphics.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chapter 17 Linked Lists.
Microsoft Access.
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
Microsoft Office Word is an example of ____ software. a. Database b
Benchmark Series Microsoft Excel 2013 Level 2
Working with Intrinsic Controls and ActiveX Controls
1..
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Analyzing Genes and Genomes
Types of selection structures
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
PSSA Preparation.
Essential Cell Biology
© Paradigm Publishing, Inc Access 2010 Level 2 Unit 2Advanced Reports, Access Tools, and Customizing Access Chapter 8Integrating Access Data.
1.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
Procedures and Functions
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
Why to Create a Procedure
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
GUI Applications JCheckBox
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
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.
Programming with Microsoft Visual Basic th Edition
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
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.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Chapter 2 Creating Applications with Visual Basic.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Sub Procedures Chapter 6-Part 1. Chapter 6 Part 12 Event Procedures Code that occurs based upon event. Mouse click Got focus Repetitive code that might.
Sub Procedures And Functions
Functions Chapter 6-Part 2.
STARTING OUT WITH Visual Basic 2008
Procedures and Functions
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6 Procedures and Functions

Copyright © 2011 Pearson Addison-Wesley Introduction A procedure is a collection of statements that performs a task – Event handlers are a type of procedure A function is a collection of statements that performs a task and returns a value to the part of the program that executed it – You have already worked with Visual Basics built-in functions, such as CInt and IsNumeric A method can be either a procedure or a function Chapter 6 – Slide 3

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Section 6.1 PROCEDURES You can write your own general purpose procedures that perform specific tasks. General purpose procedures are not triggered by events, but are called from statements in other procedures.

Copyright © 2011 Pearson Addison-Wesley Procedure Uses An event handler is a type of procedure – Automatically executed when an event such as a mouse click occurs General purpose procedures are triggered by statements in other procedures, not by events Procedures help simplify & modularize code by: – Breaking it into small, manageable pieces – Performing a task that is needed repeatedly – Dividing a program into a set of logical tasks Tutorial 6-1 examines an application with a procedure Chapter 6 – Slide 5

Copyright © 2011 Pearson Addison-Wesley Declaring a Procedure The general format of a procedure declaration is as follows: AccessSpecifier is optional and establishes accessibility to the program Sub and End are keywords ProcedureName used to refer to procedure – Use Pascal casing to capitalize 1st character of the name and each new word in the name ParameterList is a list of variables or values being passed to the sub procedure Tutorial 6-2 guides you through the process of writing procedures Chapter 6 – Slide 6 [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statements] End Sub

Copyright © 2011 Pearson Addison-Wesley Static Local Variables Variables needed only in a procedure, should be declared within that procedure – Creates a local variable with scope only within the procedure where declared – Local variable values are not saved from one procedure call to the next To save value between procedure calls, use Static keyword to create a static local variable – Scope is still only within the procedure – But variable exists for lifetime of application Chapter 6 – Slide 7 Static VariableName As DataType

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Section 6.2 PASSING ARGUMENTS TO PROCEDURES When calling a procedure, you can pass it values known as arguments.

Copyright © 2011 Pearson Addison-Wesley Arguments An Argument is value passed to a procedure For example: – Calls the CInt function – Passes txtInput.Text as an argument Two ways to pass arguments – by value is a temporary copy of the original argument – by reference is the original argument and can be changed Chapter 6 – Slide 9 CInt(txtInput.Text)

Copyright © 2011 Pearson Addison-Wesley Passing Arguments By Value intNumber declared as an integer argument Storage location intNumber created by procedure A value, 5 in this case, must be supplied and is copied into the storage location for intNumber The DisplayValue procedure then executes Tutorial 6-3 demonstrates passing arguments Chapter 6 – Slide 10 DisplayValue(5) ' Call DisplayValue procedure Sub DisplayValue(ByVal intNumber As Integer) ' This procedure displays a value in a message box. MessageBox.Show(intNumber.ToString) End Sub

Copyright © 2011 Pearson Addison-Wesley Passing Multiple Arguments Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc. Chapter 6 – Slide 11 ShowSum(intValue1, intValue2) ' Call ShowSum procedure Sub ShowSum(ByVal intNum1 As Integer, ByVal intNum2 As Integer) Dim intSum As Integer 'Local variable to hold a sum 'Get the sum of the two arguments. intSum = intNum1 + intNum2 'Display the sum. MessageBox.Show("The sum is " & intSum.ToString()) End Sub

Copyright © 2011 Pearson Addison-Wesley More about Passing Arguments by Reference Arguments are usually passed ByVal – New storage location created for procedure – Storage location gets a copy of the value – Any changes in value are made to the copy – Calling procedure wont see the changes Arguments can also be passed ByRef – Procedure points to (references) arguments original storage location – Any changes are made to the original value – Calling procedure sees the changes Tutorial 6-4 demonstrates the difference between ByVal and ByRef Chapter 6 – Slide 12

Copyright © 2011 Pearson Addison-Wesley Working with ByVal and ByRef Passing the argument ByVal – Does not change the value of intNumber Passing the argument ByRef – Allows the value of intNumber to change Chapter 6 – Slide 13

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Section 6.3 FUNCTIONS A function returns a value to the part of the program that called the function.

Copyright © 2011 Pearson Addison-Wesley Declaring a Function New keyword Function Also new is As DataType which states the data type of the value to be returned Return value is specified in a Return expression Chapter 6 – Slide 15 [AccessSpecifier] Function FunctionName ([ParameterList]) As DataType [Statements] End Function

Copyright © 2011 Pearson Addison-Wesley Function Call Example The Sum function – Passes the variables dblValue1 and dblValue2 as arguments – Data types must agree with parameter list – Assigns the value returned by the Sum function to the variable dblTotal, agrees with return value Tutorial 6-5 demonstrates function use Chapter 6 – Slide 16 dblTotal = Sum(dblValue1, dblValue2) Function Sum(ByVal dblNum1 As Double, ByVal dblNum2 As Double) As Double Return dblNum1 + dblNum2 End Function

Copyright © 2011 Pearson Addison-Wesley Returning Nonnumeric Values Functions can return nonnumeric values, such as strings and Boolean values Chapter 6 – Slide 17 strCustomer = FullName("John", "Martin") Function FullName(ByVal strFirst As String, ByVal strLast As String) As String ' Local variable to hold the full name Dim strName As String ' Append the last name to the first ' name and assign the result to strName. strName = strFirst & " " & strLast ' Return the full name. Return strName End Function

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Section 6.4 MORE ABOUT DEBUGGING: STEPPING INTO, OVER, AND OUT OF PROCEDURES AND FUNCTIONS Visual Basic debugging commands allow you to single-step through applications with procedure and function calls. The Step Into command allows you to single-step through a called procedure or function. The Step Over command allows you to execute a procedure or function call without single-stepping through its lines. The Step Out command allows you to execute all remaining lines of a procedure or function you are debugging without stepping through them.

Copyright © 2011 Pearson Addison-Wesley The Step Into Command The Step Into command – Continue to debug by single-stepping through a procedure Press the F8 key Select Debug from the menu bar, and then select Step Into from the Debug menu Click the Step Into button on the Debug Toolbar, if the toolbar is visible Tutorial 6-6 demonstrates the Step Into command Chapter 6 – Slide 19

Copyright © 2011 Pearson Addison-Wesley The Step Over Command The Step Over command – Run procedure without single-stepping, continue single-step after the call Press the Shift + F8 key Select Debug from the menu bar, and then select Step Over from the Debug menu Click the Step Over button on the Debug Toolbar, if the toolbar is visible Tutorial 6-7 demonstrates the Step Over command Chapter 6 – Slide 20

Copyright © 2011 Pearson Addison-Wesley The Step Out Command The Step Out command – End single-stepping in procedure, continue single- step after the call Press the Ctrl + Shift + F8 key Select Debug from the menu bar, and then select Step Out from the Debug menu Click the Step Out button on the Debug Toolbar, if the toolbar is visible Tutorial 6-8 demonstrates the Step Out command Chapter 6 – Slide 21

Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Section 6.5 FOCUS ON PROGRAM DESIGN AND PROBLEM SOLVING: BUILDING THE BAGEL AND COFFEE PRICE CALCULATOR APPLICATION In this section you build the Bagel and Coffee Price Calculator application. It uses procedures and functions to calculate the total of a customer order.

Copyright © 2011 Pearson Addison-Wesley Introduction The owner of Brandis Bagel House has asked you to write an application that her staff can use to record an order as it is called in Customers may call in and order – White and whole wheat bagels with a variety of toppings – Three different types of coffee The application should display – The total of the order, including 6% sales tax Bagels: – White bagel $1.25 – Whole wheat bagel $1.50 Toppings: – Cream cheese $0.50 – Butter $0.25 – Blueberry jam $0.75 – Raspberry jam $0.75 – Peach jelly $0.75 Coffee: – Regular coffee $1.25 – Cappuccino $2.00 – Café au lait $1.75 Chapter 6 – Slide 23 (Note: Delivery for coffee alone is not offered.)

Copyright © 2011 Pearson Addison-Wesley Sketch of Brandis Bagel House Form Chapter 6 – Slide 24

Copyright © 2011 Pearson Addison-Wesley Description of Click Event Handlers Chapter 6 – Slide 25 NameDescription btnCalculate_Click Calculates and displays the total of an order Calls the following functions: BagelCost, CoffeeCost, ToppingCost, and CalcTax btnExit_ClickEnds the application btnReset_Click Resets the controls on the form to their initial values Calls the following procedures: ResetBagels, ResetToppings, ResetCoffee, and ResetPrice

Copyright © 2011 Pearson Addison-Wesley btnCalculate_Click Flowchart and Pseudocode Chapter 6 – Slide 26 subtotal = BagelCost() + ToppingCost() + CoffeeCost() tax = CalcTax(subtotal) total = subtotal + tax lblSubtotal.Text = subtotal lblTax.Text = tax lblTotal.Text = total

Copyright © 2011 Pearson Addison-Wesley btnReset_Click Flowchart and Pseudocode Chapter 6 – Slide 27 ResetBagels() ResetToppings() ResetCoffee() ResetPrice()

Copyright © 2011 Pearson Addison-Wesley Description of Functions Chapter 6 – Slide 28 NameDescription BagelCostReturns the price of the selected bagel ToppingCostReturns the total price of the selected toppings CoffeeCostReturns the price of the selected coffee CalcTax Accepts the amount of a sale as an argument Returns the amount of sales tax on that amount The tax rate is stored in a class-level constant, decTAX_RATE

Copyright © 2011 Pearson Addison-Wesley BagelCost Function Flowchart and Pseudocode Chapter 6 – Slide 29 If White Is Selected Then cost of bagel = 1.25 Else cost of bagel = 1.5 End If Return cost of bagel

Copyright © 2011 Pearson Addison-Wesley ToppingCost Function Flowchart and Pseudocode Chapter 6 – Slide 30 cost of topping = 0.0 If Cream Cheese Is Selected Then cost of topping += 0.5 End If If Butter Is Selected Then cost of topping += 0.25 End If If Blueberry Is Selected Then cost of topping += 0.75 End If If Raspberry Is Selected Then cost of topping += 0.75 End If If Peach Is Selected Then cost of topping += 0.75 End If Return cost of topping

Copyright © 2011 Pearson Addison-Wesley CoffeeCost Function Flowchart and Pseudocode Chapter 6 – Slide 31 If No Coffee Is Selected Then cost of coffee = 0 ElseIf Regular Coffee Is Selected Then cost of coffee = 1.25 ElseIf Cappuccino Is Selected Then cost of coffee = 2 ElseIf Café Au Lait Is Selected Then cost of coffee = 1.75 End If Return cost of coffee

Copyright © 2011 Pearson Addison-Wesley CalcTax Function Flowchart and Pseudocode Chapter 6 – Slide 32 sales tax = amount * tax rate Return sales tax

Copyright © 2011 Pearson Addison-Wesley Description of Procedures Chapter 6 – Slide 33 NameDescription ResetBagelsResets the bagel type radio buttons to their initial value ResetToppingsResets the topping check boxes to unchecked ResetCoffeeResets the coffee radio buttons to their initial values ResetPrice Sets the Text property of the lblSubtotal, lblTax, and lblTotal labels to String.Empty

Copyright © 2011 Pearson Addison-Wesley ResetBagels Procedure Flowchart and Pseudocode Chapter 6 – Slide 34 radWhite = Selected radWheat = Deselected

Copyright © 2011 Pearson Addison-Wesley ResetToppings Procedure Flowchart and Pseudocode Chapter 6 – Slide 35 chkCreamCheese = Unchecked chkButter = Unchecked chkBlueberry = Unchecked chkRaspberry = Unchecked chkPeach = Unchecked

Copyright © 2011 Pearson Addison-Wesley ResetCoffee Procedure Flowchart and Pseudocode Chapter 6 – Slide 36 radNoCoffee = Deselected radRegCoffee = Selected radCappuccino = Deselected radCafeAuLait = Deselected

Copyright © 2011 Pearson Addison-Wesley ResetPrice Procedure Flowchart and Pseudocode Chapter 6 – Slide 37 lblSubtotal.Text = String.Empty lblTax.Text = String.Empty lblTotal.Text = String.Empty

Copyright © 2011 Pearson Addison-Wesley Brandis Bagel House Form Chapter 6 – Slide 38