Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
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
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
An Introduction to Programming with C++ Fifth Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
Microsoft Visual Basic: Reloaded Chapter Eight Sub and Function Procedures.
Chapter 7: Sub and Function Procedures
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
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 th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
Microsoft Visual Basic 2005: Reloaded Second Edition
Tutorial 11 Using and Writing Visual Basic for Applications Code
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Why to Create a Procedure
Programming with Microsoft Visual Basic 2008 Fourth Edition
CS0004: Introduction to Programming Subprocedures and Modular Design.
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 One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 6 Sub Procedures
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Creating Classes and Objects Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
© 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.
1.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Three Using Variables and Constants.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Programming with Microsoft Visual Basic th Edition
Chapter 8: Advanced Method Concepts. Understanding Parameter Types Mandatory parameter – An argument for it is required in every method call Four types.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
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.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 9: Value-Returning Functions
Chapter 10: Void Functions
Functions Chapter 6-Part 2.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 6 Sub Procedures
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Visual Programming
Chapter 9: Value-Returning Functions
Chapter 8 - Functions and Functionality
STARTING OUT WITH Visual Basic 2008
Chapter 10: Void Functions
Presentation transcript:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures

Microsoft Visual Basic 2005: Reloaded, Second Edition2 Objectives After studying this chapter, you should be able to: Explain the difference between a Sub procedure and a Function procedure Create a Sub procedure Create a procedure that receives information passed to it Explain the difference between passing data by value and passing data by reference

Microsoft Visual Basic 2005: Reloaded, Second Edition3 Objectives (continued) Explain the purpose of the sender and e parameters Associate a procedure with more than one object and event Create a Function procedure Convert an object to a different type using the TryCast keyword

Microsoft Visual Basic 2005: Reloaded, Second Edition4 Procedures Procedure: a block of program code that performs a specific task Two types of procedures: –Function procedure: returns a value after performing its task –Sub procedure: does not return a value

Microsoft Visual Basic 2005: Reloaded, Second Edition5 Sub Procedures Event procedure: –Sub procedure that is associated with a specific object and event –Automatically processed when the associated event occurs Independent Sub procedure: –Collection of code that can be invoked from one or more places in an application –Not associated with an event –Processed only when called (invoked)

Microsoft Visual Basic 2005: Reloaded, Second Edition6 Sub Procedures (continued) Independent Sub procedures: –Eliminate the duplication of code in different parts of a program –Allow a large, complex application to be broken into small and manageable tasks –Allow multiple programmers to work on an application simultaneously

Microsoft Visual Basic 2005: Reloaded, Second Edition7 Sub Procedures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition8 Sub Procedures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition9 Sub Procedures (continued) Independent Sub procedures: –Have a procedure header and procedure footer –Use Pascal case for names –Optionally contain a parameter list Parameters: –Declared in the procedure header –Store the information passed into the procedure when it is invoked Call statement: invokes an independent Sub procedure

Microsoft Visual Basic 2005: Reloaded, Second Edition10 Sub Procedures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition11 The Gadis Antiques Application

Microsoft Visual Basic 2005: Reloaded, Second Edition12 The Gadis Antiques Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition13 The Gadis Antiques Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition14 The Gadis Antiques Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition15 Including Parameters in an Independent Sub Procedure Parameter: stores data that is passed to the procedure when the procedure is invoked When invoking a procedure with parameters, you must pass: –The same number of parameters –The same type of parameters –The parameters in the same order as declared in the procedure Can pass a variable, named constant, literal constant, or keyword as parameter

Microsoft Visual Basic 2005: Reloaded, Second Edition16 Passing Variables Each variable has a value and a unique memory address Variable can be passed to a procedure in two ways: –By value: you pass the variable’s value –By reference: you pass the variable’s address By value: the procedure receives only the value and cannot change the actual variable’s value By reference: the procedure receives the address, and can make changes to the variable’s value

Microsoft Visual Basic 2005: Reloaded, Second Edition17 Passing Variables by Value Use the keyword ByVal before the parameter in the procedure declaration ByVal is the default method of passing variables Procedure cannot change the actual variable’s value

Microsoft Visual Basic 2005: Reloaded, Second Edition18 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition19 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition20 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition21 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition22 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition23 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition24 Passing Variables by Value (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition25 Passing Variables by Reference Use the keyword ByRef before the parameter in the procedure declaration Procedure receives the address of the variable, and is able to change the variable’s value

Microsoft Visual Basic 2005: Reloaded, Second Edition26 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition27 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition28 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition29 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition30 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition31 Passing Variables by Reference (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition32 Associating a Procedure with Different Objects and Events Handles keyword: –Appears in event procedure header –Indicates the object and event associated with the procedure –Controls when the procedure is invoked By default, the event procedure name matches the name of the associated object and event

Microsoft Visual Basic 2005: Reloaded, Second Edition33 Associating a Procedure with Different Objects and Events (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition34 Associating a Procedure with Different Objects and Events (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition35 Associating a Procedure with Different Objects and Events (continued) Event procedure: –Name of event procedure can be changed –Can be associated with more than one object and event as long as each event has the same parameters Add the additional object/events to the Handles clause Sender parameter: contains the memory address of the object that raised the event e parameter: contains additional information about the object that raised the event

Microsoft Visual Basic 2005: Reloaded, Second Edition36 Associating a Procedure with Different Objects and Events (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition37 Function Procedures Function procedure (or Function): –Block of code that performs a specific task –Returns a value after completing its task Visual Basic contains many built-in functions You can create your own functions with or without parameters

Microsoft Visual Basic 2005: Reloaded, Second Edition38 Function Procedures (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition39 Function Procedures (continued) Function procedure header: –As datatype clause indicates the type of the return value Function procedure footer statement: –End Function Return keyword: –Sets the value to be returned by the function –Ends the function

Microsoft Visual Basic 2005: Reloaded, Second Edition40 The Pine Lodge Application

Microsoft Visual Basic 2005: Reloaded, Second Edition41 The Pine Lodge Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition42 The Pine Lodge Application (continued)

Microsoft Visual Basic 2005: Reloaded, Second Edition43 Programming Tutorial

Microsoft Visual Basic 2005: Reloaded, Second Edition44 Programming Example

Microsoft Visual Basic 2005: Reloaded, Second Edition45 Summary Function procedures return a value; Sub procedures do not return a value Event procedure: a Sub procedure associated with one or more objects and events Independent Sub and Function procedures: not associated with any specific object or event Call statement: used to invoke a procedure When calling a procedure, you must pass the same number, type, and order of parameter values as those declared in the procedure

Microsoft Visual Basic 2005: Reloaded, Second Edition46 Summary (continued) Values can be passed to a procedure by value or by reference By Value: –Provides only the value of the variable to the procedure –Use the ByVal keyword By Reference: –Provides the address of the variable to the procedure, allowing the procedure to change the variable’s value –Use the ByRef keyword

Microsoft Visual Basic 2005: Reloaded, Second Edition47 Summary (continued) Variables in the parameter list in a procedure header have procedure-level scope TryCast keyword: allows you to convert an object from one data type to another