Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
1.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
An Introduction to Programming with C++ Fifth Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Example 2.
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.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Object-Oriented Application Development Using VB.NET 1 Chapter 7 Adding Responsibilities to Problem Domain Classes.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 7: Working with Arrays
VB .NET Programming Fundamentals
CS708 Fall 2004 Professor Douglas Moody –MW – 2:15-3:55 pm –Friday – 6:00-9:20 pm – – –Web Site: websupport1.citytech.cuny.edu.
Chapter 6: Functions.
Apply Sub Procedures/Methods and User Defined Functions
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Chapter 10: Writing Class Definitions Visual Basic.NET Programming: From Problem Analysis to Program Design.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Applications Development
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Chapter 9: Value-Returning Functions
IS 350 Application Structure
About the Presentations
Method.
Group Status Project Status.
CIS16 Application Development and Programming using Visual Basic.net
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Chapter 9: Value-Returning Functions
Methods.
Presentation transcript:

Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic.NET Programming: From Problem Analysis to Program Design2 Objectives Explore the client-server model Review the syntax of procedures Write user-defined procedures Design and write overloaded procedures

Visual Basic.NET Programming: From Problem Analysis to Program Design3 Exploring the Client-Server Model Methods –Procedures within class definition –Provide services to other objects in system Objects send messages to invoke procedures –Client object sends message –Server object receives message

Visual Basic.NET Programming: From Problem Analysis to Program Design4

5 Exploring the Client-Server Model (continued) Client sends message invoking server procedure –Optionally sending values in form of arguments Server procedure performs requested task –Optionally returns value to client

Visual Basic.NET Programming: From Problem Analysis to Program Design6 Exploring the Client-Server Model (continued) Calling method example: –Console.WriteLine(“Hello World Wide Web”) Class: Console Method: WriteLine Argument: “Hello World Wide Web”

Visual Basic.NET Programming: From Problem Analysis to Program Design7 Exploring the Client-Server Model (continued) Calling method example: –doubleResult = Math.Sqrt(firstInt) Class: Math Method: Sqrt Argument: firstInt Return value: doubleResult

Visual Basic.NET Programming: From Problem Analysis to Program Design8 Exploring the Client-Server Model (continued) Unified Modeling Language (UML) sequence diagram –Maps interactions between objects –Shows objects as rectangles –Lifelines Vertical lines below objects Represent sequence of time Shown as either dashed line or narrow box

Visual Basic.NET Programming: From Problem Analysis to Program Design9

10 Exploring the Client-Server Model (continued) Unified Modeling Language (UML) sequence diagram (continued) –Active object Object executing or controlling part of interaction –Messages Represented using arrows

Visual Basic.NET Programming: From Problem Analysis to Program Design11 Example 9-1: Invoking Math.Pow 1. Option Explicit On 2. Option Strict On 3. Module Chapter9Example1 4. Sub Main() 5. Dim answer As Double 6. answer = Math.Pow(4, 2) 7. Console.WriteLine(answer) 8. End Sub 9. End Module

Visual Basic.NET Programming: From Problem Analysis to Program Design12

Visual Basic.NET Programming: From Problem Analysis to Program Design13 Reviewing the Syntax of Procedures Using procedures –Improves code clarity –Enhances error detection –Simplifies maintenance Types of procedure in VB.NET: –Sub –Function

Visual Basic.NET Programming: From Problem Analysis to Program Design14 Using Sub Procedures Sub procedure –Begins with procedure header –Followed by one or more statements –Ends with keywords End Sub

Visual Basic.NET Programming: From Problem Analysis to Program Design15

Visual Basic.NET Programming: From Problem Analysis to Program Design16 Using Sub Procedures (continued) Procedure header parts: –Accessibility –Sub –Procedure name –Parameter list

Visual Basic.NET Programming: From Problem Analysis to Program Design17 Using Sub Procedures (continued) Parameters –Values passed to procedure –Declared using keywords ByVal ByRef

Visual Basic.NET Programming: From Problem Analysis to Program Design18 Using Sub Procedures (continued) By value –Procedure does not have direct access to argument variable Cannot change its contents –Procedure is given copy of variable By reference –Procedure code given direct access to argument variable May change its value

Visual Basic.NET Programming: From Problem Analysis to Program Design19 Using Function Procedures Function procedure definition –Begins with procedure header –Followed by one or more statements –Ends with keywords End Function

Visual Basic.NET Programming: From Problem Analysis to Program Design20

Visual Basic.NET Programming: From Problem Analysis to Program Design21 Using Function Procedures (continued) Procedure header: –Accessibility –Function –Procedure name –Parameter list –As dataType

Visual Basic.NET Programming: From Problem Analysis to Program Design22 Example 9-3: Writing and invoking a Function procedure 1. Option Explicit On 2. Option Strict On 3. Module Client 4. Sub Main() 5. Dim total As Integer 6. total = Server.AddEmUp(2, 3) 7. Console.WriteLine(total) 8. End Sub 9. End Module

Visual Basic.NET Programming: From Problem Analysis to Program Design23 Example 9-3: Writing and invoking a Function procedure (continued) 3. Module Server 4. Public Function AddEmUp(ByVal a As Integer, ByVal b As Integer) As Integer 5. Dim sum As Integer 6. sum = a + b 7. Return sum 8. End Sub 9. End Module

Visual Basic.NET Programming: From Problem Analysis to Program Design24 Example 9-3: Writing and invoking a Function procedure (continued) Returning a variable –Use keyword Return –Followed by variable –Returns control of execution to invoking client

Visual Basic.NET Programming: From Problem Analysis to Program Design25

Visual Basic.NET Programming: From Problem Analysis to Program Design26 Writing User-Defined Procedures User-defined procedures –Procedures that you design and write –Contrast to methods contained in FCL classes –Use to give code structure and organization –Can be Private or Public

Visual Basic.NET Programming: From Problem Analysis to Program Design27 Writing User-Defined Procedures (continued) Private procedures –Provide services only to module in which they are defined –Cannot be invoked by code in other objects Public procedures –Designed specifically to be invoked by other objects

Visual Basic.NET Programming: From Problem Analysis to Program Design28 Writing Private Procedures From Example 9-4: 13. Private Sub InputFahrenheit() 14. Console.WriteLine("Please enter a Fahrenheit temperature:") 15. fahrenheit = Convert.ToDouble(Console.ReadLine()) 16. End Sub

Visual Basic.NET Programming: From Problem Analysis to Program Design29 Writing Private Procedures (continued) Scope: –Determined by where variable is declared –Controls where variable can be accessed Module scope –Variable declared within module –Can be accessed by code in module

Visual Basic.NET Programming: From Problem Analysis to Program Design30 Writing Private Procedures (continued) Procedure scope –Variable declared within procedure –Only code within procedure can access variable Block scope –Declared within If, Do, or For statement –Can only be accessed within block

Visual Basic.NET Programming: From Problem Analysis to Program Design31 Writing Private Procedures (continued) Use variables with smallest or narrowest scope possible –Minimizes impact that changes in one procedure may have in others Hide internal workings of procedure from outside code Public procedures can be invoked by any object Design public procedures to provide services that are required by multiple objects or applications

Visual Basic.NET Programming: From Problem Analysis to Program Design32 Using Optional Parameters May sometimes want to make some arguments optional Use keyword Optional –In parameter declaration –Provide default value –Must be placed at end of parameter list

Visual Basic.NET Programming: From Problem Analysis to Program Design33 Example 9-7: Using an Optional Parameter Excerpt: Public Function ComputeShippingCharge(Optional ByVal distance As Integer = 100) As Double

Visual Basic.NET Programming: From Problem Analysis to Program Design34 Designing and Writing Overloaded Procedures Procedure’s signature consists of –Name –Parameter list VB.NET identifies procedures by signature –Can have two or more procedures with same name –As long as parameter lists are different

Visual Basic.NET Programming: From Problem Analysis to Program Design35 Designing and Writing Overloaded Procedures (continued) Overloading a procedure –Write multiple procedures with same name but with different signatures –Convenient technique to avoid using unique names for procedures –Use Overloads keyword Return data type is not part of procedure’s signature

Visual Basic.NET Programming: From Problem Analysis to Program Design36 Example 9-8: Overloading a procedure (Excepts) Public Overloads Function ComputeSalesTax(ByVal amount As Single) As Double Public Overloads Function ComputeSalesTax(ByVal amount As Double) As Double

Visual Basic.NET Programming: From Problem Analysis to Program Design37 Programming Example: Electricity Billing Input –Previous and current meter reading Output –Electric bill containing: 1.Kilowatt hours (kwh) consumed 2.Electric charge formatted as currency 3.Sales tax formatted as currency 4.Bill total formatted as currency

Visual Basic.NET Programming: From Problem Analysis to Program Design38 Programming Example: Electricity Billing (continued) Problem Analysis and Algorithm Design –Purpose of program: Input previous and current electric meter readings Compute and display total bill –Using sentinel-controlled loop

Visual Basic.NET Programming: From Problem Analysis to Program Design39 Programming Example: Electricity Billing (continued) Procedures used: –Main –GetPreviousReading –GetCurrentReading –ComputeElectricityCharge: –DisplayBill

Visual Basic.NET Programming: From Problem Analysis to Program Design40 Summary Two types of procedures in Visual Basic.NET: –Sub procedures –Function procedures Arguments are passed into parameters Two categories of user-defined procedures: –Public –Private

Visual Basic.NET Programming: From Problem Analysis to Program Design41 Summary (continued) Scope of variable –Represents visibility or accessibility –Determined by where you declare it Create optional argument by adding keyword Optional Procedure’s signature consists –Name –Parameter list

Visual Basic.NET Programming: From Problem Analysis to Program Design42 Summary (continued) Overloading procedure: –Write multiple procedures with same name but different signatures