1.

Slides:



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

1.
Sub and Function Procedures
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
1.
Chapter 11: Classes and Objects
Programming with Microsoft Visual Basic th Edition
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
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.
Example 2.
Chapter 7: Sub and Function Procedures
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
1.
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.
Chapter Four The Selection Structure
Chapter 4: The Selection Structure
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.
Why to Create a Procedure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Chapter 4: The Selection Structure
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
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.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
Chapter 6: The Repetition Structure
Tutorial 6 The Repetition Structure
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Programming with Microsoft Visual Basic th Edition
© 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.
Chapter Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
Programming with Microsoft Visual Basic th Edition
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Course ILT Using complex selection structures Unit objectives Include radio buttons and check boxes in an interface, create and call a user- defined Sub.
Value-Returning Functions
Chapter 3: Using Variables and Constants
Functions Chapter 6-Part 2.
Programming with Microsoft Visual Basic 2008 Fourth Edition
CIS16 Application Development and Programming using Visual Basic.net
Chapter 9: Value-Returning Functions
Chapter 10: Void Functions
Presentation transcript:

1

Objectives 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 Create a Function procedure Microsoft Visual Basic .NET: Reloaded

Procedure Procedure: a block of program code that performs a specific task Function Procedure Returns a value after performing its assigned task Sub Procedure Completes the task but does not return a value Microsoft Visual Basic .NET: Reloaded

Sub Procedures Event procedure Independent Sub procedure Associated with a specific object and event Example: button click event Independent Sub procedure A collection of code that can be invoked from one or more places in an application Processed only when called (invoked) from code Invoke Sub using a Call statement from another procedure Microsoft Visual Basic .NET: Reloaded

Sub Procedures (continued) Private indicates procedure can only be used by other procedures in the current form Sub keyword identifies procedure as a Sub Procedure procedurename - name given to procedure Naming rules same as for naming variables parameterlist - optional set of memory locations referred to as parameters ByVal, ByRef specify how parameter is passed Microsoft Visual Basic .NET: Reloaded

Sub Procedures (continued) Microsoft Visual Basic .NET: Reloaded

Sub Procedures (continued) Microsoft Visual Basic .NET: Reloaded

Gladis Antiques Application Manager wants an application to calculate employee’s regular pay, overtime pay, and gross pay Employees are paid on hourly basis with time and one-half for overtime Sub ClearLabels() will be used to clear display labels on form for 3 pay amounts Microsoft Visual Basic .NET: Reloaded

Gladis Antiques Application (continued) Microsoft Visual Basic .NET: Reloaded

Gladis Antiques Application (continued) Microsoft Visual Basic .NET: Reloaded

Gladis Antiques Application (continued) Microsoft Visual Basic .NET: Reloaded

Including Parameters in an Independent Sub Procedure Call statement has an optional argumentlist Comma separated list of arguments passed to procedure being called Argumentlist must agree with number of parameters listed in parameterlist in the procedure header Data type and position of each parameter must agree with data type and position of corresponding argument Microsoft Visual Basic .NET: Reloaded

Passing Variables Pass by value Pass by reference Use keyword ByVal Passes contents of variable to the parameter in receiving procedure but not memory location Cannot change contents of passing variable Pass by reference Use keyword ByRef Passes memory address of variable to the receiving procedure Contents of variable can be changed within receiving procedure Microsoft Visual Basic .NET: Reloaded

Passing Variables by Value Microsoft Visual Basic .NET: Reloaded

Passing Variables by Value (continued) Microsoft Visual Basic .NET: Reloaded

Passing Variables by Reference Microsoft Visual Basic .NET: Reloaded

Passing Variables by Reference (continued) Microsoft Visual Basic .NET: Reloaded

Passing Variables by Reference (continued) Microsoft Visual Basic .NET: Reloaded

Function Procedures Block of code that performs a task and returns a value after completing that task Function header has keyword Function instead of Sub Header has “As datatype” clause that specifies data type of value returned by the function As Decimal - returns a decimal number As String - returns a string Return statement alerts computer that function has completed task and returns the value contained in its expression Microsoft Visual Basic .NET: Reloaded

Function Procedures (continued) Microsoft Visual Basic .NET: Reloaded

The Pine Lodge Application Owner wants application to calculate new hourly pay given current pay rate and raise rate Application demonstrates use of function procedure GetNewPay Current pay rate and raise rate passed by value as parameters New hourly pay returned by function Microsoft Visual Basic .NET: Reloaded

The Pine Lodge Application (continued) Microsoft Visual Basic .NET: Reloaded

The Pine Lodge Application (continued) Microsoft Visual Basic .NET: Reloaded

The Pine Lodge Application (continued) Microsoft Visual Basic .NET: Reloaded

Programming Example – Rainfall Application Application allows user to enter monthly rainfall amounts for previous year Calculates and displays the total rainfall amount and average rainfall amount Microsoft Visual Basic .NET: Reloaded

TOE Chart Microsoft Visual Basic .NET: Reloaded

User Interface Microsoft Visual Basic .NET: Reloaded

Objects, Properties, and Settings Microsoft Visual Basic .NET: Reloaded

Tab Order Microsoft Visual Basic .NET: Reloaded

Pseudocode btnExit Click event procedure btnCalc Click event procedure 1. Close the application btnCalc Click event procedure 1. call the CalctotalAndAverage procedure to calculate the total and average rainfall 2. display the total and average rainfall amounts in labels Microsoft Visual Basic .NET: Reloaded

Pseudocode (continued) CalcTotalAndAverage procedure 1. initialize the month counter to 1 2. repeat while amount is numeric get a rainfall amount if rainfall amount is numeric add rainfall amount to total rainfall accumulator add 1 to the month counter else display message informing user to enter a number end if end repeat 3. calculate the average rainfall = total rainfall / 12 Microsoft Visual Basic .NET: Reloaded

Code Microsoft Visual Basic .NET: Reloaded

Code (continued) Microsoft Visual Basic .NET: Reloaded

Summary Procedures allow reuse of code throughout an application and allow programmer teamwork on large and complex applications Function procedures return a value, Sub procedures do not Independent Sub procedures and Functions are not associated with any specific object or event Use Call statement to invoke an Independent sub procedure Microsoft Visual Basic .NET: Reloaded

Summary (continued) Number of arguments and data types must match in argumentlist and parameterlist ByVal keyword passes contents of variable Value of variable being passed cannot be changed in procedure ByRef keywords passes the memory address Value of variable being pass can be changed in procedure Variables in parameterlist have procedure level scope Microsoft Visual Basic .NET: Reloaded