Download presentation
Presentation is loading. Please wait.
1
STARTING OUT WITH Visual Basic 2008
FOURTH EDITION Tony Gaddis Haywood Community College Kip Irvine Florida International University
2
Procedures And Functions
Chapter Procedures And Functions 6 Both Procedures and Functions are collections of statements that perform some task. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
3
Introduction A procedure is a collection of statements that performs a task. A function is a collection of statements that performs a task. It is a special type of procedure. Procedures vs. Functions Procedure: One example is a button click procedure. Function: Two examples are CDec() and FormatCurrency()
4
Procedures and Functions
OBJECTIVES: How to write procedures. How to write functions. Learn the importance of procedures and functions. Passing variable values from one procedure to another By value By reference First introduction to structured design: Coupling Cohesion Called Methods in OOP Heart and soul of structured design. 4
5
Procedures and Functions allow programmers to: Divide and Conquer
IMPORTANT: Knowledge of procedures and functions is far, far more important than your book indicates. 5
6
Divide and Conquer Divide and Conquer
To build a safe and reliable jumbo jet: 1. We must have a carefully designed overall plan. 2. We must break the plane down into smaller units, each of which has a SINGLE FUNCTION. 3. We must carefully design these smaller units. 4. We must thoroughly test these smaller units. 5. We must assemble these smaller units into a whole plane. 6. We must thoroughly test the whole plane. Each of these units will be further broken down into smaller and smaller units each with a SINGLE FUNCTION. For example the jet engine is broken down into hundreds of smaller units, each with a SINGLE FUNCTION. 6
7
To break down the program into smaller units we will use:
Divide and Conquer Building a large software application follows the "Divide and Conquer" strategy. To break down the program into smaller units we will use: 1. Event procedures. - You have already used these, such as the Button Click sub-procedure. - You will NOT learn to write these. These are too complex for this course. - You will learn to write these to do a SINGLE FUNCTION. 2. General procedures - You will learn to write these soon and they will do a SINGLE FUNCTION. 3. Function Procedures (Yes, Functions are a type of procedure. - You have already used built-in functions, such as the FormatCurrency. SOME SINGLE FUNCTION EXAMPLES: 1. Sort a list of names. 2. Search for a name in a list. 3. Calculate interest over any number of years. 4. Check for input errors. 5. Format a number into currency 6. Convert a number stored as text into a decimal value. 7. Find the square root of a number. 8. Print headings on a report. 9. Find the standard deviation of a list of numbers. 10. Calculate the sales commission. 7
8
Divide and Conquer DEFINITION OF A SINGLE FUNCTION:
1. At this time, I can't give you a definition. Yes, you read the words correctly. 2. At this time, I can only give you examples of a single function. (See previous slide.) However, I can give you a way to test for a single function: (1) If you can say that the procedure "does this OR it does that", then the procedure "may" have more than one function. (2) If you can say that the procedure "does this AND it does that", then the procedure "may" have more than one function. 3. Believe it or not, you must learn what a single function is not, then you will know what it is. (More on this later.) 8
9
Definition of “Module”
MODULE DEFINITION Historically, the word module has and will continue to mean a unit of code such as: 1. The code in a procedure (also called a sub procedure) 2. The code in a function (a function is just special type of procedure) 3. The code written without a form associated with it. 4. Historically, we tried to limit the size of the modules to the amount of code need to perform a SINGLE FUNCTION. MIS 15 will adhere to this SINGLE FUNCTION rule. In the slides to follow, the word “module” will be used in its historical sense, and will involve small units of code that perform a “single function.” Therefore, procedures, as well as, functions will be referred to as modules. 9
10
Modularizing using Arguments and Parameters
6.2 Sending a copy of a value from one procedure of a program to another procedure within the program. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10
11
Modularizing using Arguments and Parameters
This program demonstrates: 1. Hierarchy Charts 2. Procedures 3. Arguments and Parameters 4. Passing by Value 5. Control of “Coupling” IMPORTANT: Knowledge of procedures and functions is far, far more important than your book indicates. 11
12
Modularizing using Arguments and Parameters
ByVal Ways to pass values from one procedure to another procedure: 1. ByVal 2. ByRef Passing By Value (ByVal) These are displayed results from a procedure called btnPassValue. This procedure passes 10.75 to the DisplayValue procedure below. This procedure (DisplayValue) recieves 10.75 from the btnPassValue procedure and displays it and changes the value (10.75) to and displays it. IMPORTANT: Knowledge of procedures and functions is far, far more important than your book indicates. 12
13
Modularizing using Arguments and Parameters
ByVal Red indicates and Event What is a Hierarchy Chart? 1. Purpose: It shows the relationships between procedures (modules) of code. 2. Each procedure (module) is named according to its single function. 3. The Pass Value procedure is a button click procedure that calls the Display Value procedure. 4. The Pass Value procedure also sends a copy of the value to Display Value for further processing. Hierarchy Chart Pass Value 10.75 Copy Display Value Note: The designer of the program does their best to use a very descriptive name for each procedure (module). This often involves a lot of thinking. 13
14
Modularizing using Arguments and Parameters
ByVal Hierarchy Chart 1. The two rectangles in the Hierarchy chart are represent in the two coded procedures below. 2. The Call statement below calls the Display Value procedure and passes to it. Hierarchy Chart Pass Value 10.75 Display Value 10.75 14
15
Modularizing using Arguments and Parameters
ByVal Arguments and Parameters 1. This program is passing a copy of a value (10.75) in the memory location decOriginal to a memory location called decCopy. decCopy will hold until it is assigned in the DisplayValue procedure. Note that decOriginal is never affected by changes to decCopy. 2. A copy of the Value is passed from the Argument (decOriginal) to the Parameter called (decCopy). Argument decOriginal 10,75 Argument Event Procedure Parameter 10.75 Parameter decCopy 10,75 General Procedure decCopy 12.55 15
16
Purposes for Arguments and Parameters
ByVal Purposes for Arguments and Parameters 1. They allow for communication of information between modules of code, such as, between procedures (both general procedures and function procedures). They communicate by passing data from one module to another through arguments and parameters. 2. Their major purpose: Control of “coupling” -- Coupling occurs when modules of code share data using module-level (class-level) variables or TOO MANY ByRef parameters. -- Thus, rather than use module-level (class-level) variables to communicate between modules, such as procedures, we use arguments and parameters. -- Coupling and Cohesion are the “heart and soul” of structured design. When programming, we want to keep our modules “loosely” coupled. As for Cohesion, it is an attempt to keep our modules at the functional level, that is, one function per module. Instructor: Give examples why module-level (class-level) variables are to be avoided. 16
17
Passing a copy of a value
ByVal Passing a copy of a value A Metaphor: Mary made a copy of an original document that has a value of on it and passes the copy to Matt. Matt erases the on his copy and replaces it with QUESTIONS: 1. Was the original that Mary is holding, altered by Matt's actions? 2. Why wasn't the original affected? Mary Argument Matt Yes, I erased the 10.75 value on the copy and replaced it with Parameter Copy 10.75 Original Copy Mary passes a copy of the value to Matt Copy Slide 6- 17 17
18
Passing a copy of a value
ByVal Passing a copy of a value IN THE CODE BELOW: 1. The value (10.75) in a location in memory called decOriginal is copied and sent to a location in memory called decCopy. 2. The value (10.75) in decCopy is changed to QUESTION: 1. When decCopy was changed to 12.55, did the value in decOrginal change. Passing By Value (ByVal) 10.75D Are these notes seen Slide 6- 18 18
19
Flow of Control when DisplayValue is Called
ByVal Flow of Control when DisplayValue is Called FLOW OF CONTROL When DisplayValue is called, the following things happen in order: The control goes to the DisplayValue procedure The decCopy variable location in memory is created as a Decimal The value (10.75) in decOriginal is passed into the memory location, decCopy. All the code in DisplayValue is executed and control returns to the statement immediately following the Call statement. 10.75D Are these notes seen Passing By Value (ByVal) Slide 6- 19 19
20
Modularizing using Arguments and Parameters
ByRef Ways to pass values from one procedure to another procedure: 1. ByVal 2. ByRef Passing By Reference (ByRef) Output from the PassValue procedure Output from the DisplayValue procedure IMPORTANT: Knowledge of procedures and functions is far, far more important than your book indicates. 20
21
Modularizing using Arguments and Parameters
ByRef ByRef Passing ByRef (ByReference) -- When passing by reference the LOCATION (memory address) of the the Argument’s value in one procedure is sent to that Argument’s corresponding Parameter in another procedure. -- With ByRef, the Parameter holds the memory Location of the Argument. IMPORTANT: Knowledge of procedures and functions is far, far more important than your book indicates. 21
22
Flow of Control when DisplayValue is Called
ByRef Flow of Control when DisplayValue is Called FLOW OF CONTROL When DisplayValue is called, the following things happen in order: The control goes from the PassValue procedure to the DisplayValue Procedure. The Argument, decOriginal, passes it’s location in memory to the decLocationofOriginal Paramenter. Assume that 100 is the actual physical address of decOriginal. 3. Any value assignment to the Parameter is automatically placed in the Argument’s location, 100. decOriginal Memory Address of decOriginal: 100 10.75D Value held at this address Are these notes seen Location of the Argument is passed decLocationOfOriginal 100 Slide 6- 22 Holds the address of decOriginal, the Argument. 22
23
Passing the Address of an Argument to a Parameter
ByRef Passing the Address of an Argument to a Parameter A Metaphor: Mary (Argument) sent a note to Matt (Parameter) informing him that she is holding the original value. If he wants to change the original to 12.55, Matt will need to find Mary, the Argument. Assume that Matt is the Parameter and is holding the Location of the Argument, Mary. QUESTIONS: 1. What information about Mary does Matt (the Parameter) have? 2. How can Matt (the Paramenter) change the value of the Argument. Yes, I understand. I will need to locate Mary and change the original value (10.75) to the new value of 12.55 Mary Argument Matt Parameter Location of original Original with value: Mary passes a note with the location of the original value. Mary’s location Slide 6- 23 23
24
Passing Multiple Arguments
ByVal & ByRef Passing Multiple Arguments ByVal & ByRef What this program does: 1. Inputs a number 2. Cubes the number 3. Displays the number on the form. 4. It demonstrated using Multiple Arguments Are these notes seen Slide 6- 24 24
25
Passing Multiple Arguments
ByVal & ByRef Passing Multiple Arguments Main Concepts of Program 1. The Boss Calls the GetNumber procedure that gets and returns the inputted decNumber to the boss module using ByRef. 2. The Boss module passes decNumber to the GetNumberCubed procedure using ByVal. This procedure cubes decNumber and passes decNumberCubed ByRef to the Boss. 3. The Boss passes ByVal decNumberCubed to the DisplayNumberCubed procedure for displaying decNumberCubed on Form1. Are these notes seen Slide 6- 25 25
26
Passing Multiple Arguments
ByVal & ByRef Passing Multiple Arguments Are these notes seen Slide 6- 26 26
27
SUMMARY The arguments and their parameters must match in three ways:
1. Data type -Example-1: If an argument is an integer, its parameter must be an integer. -Example-2: If an argument is a decimal, its parameter must be a decimal. -Example-3:If an argument is a string, its parameter must be a string. 2. Position -Example: If argument-2 is second in the list of arguments, then its corresponding parameter-2 must be second in the parameter list. 3. Number -Example: If there are 3 arguments, then there must be 3 parameters. Arguments and their corresponding parameters need not match on: 1. Names -Example: Note the different names below. However, for this class, Dr. Scanlan wants you to ALWAYS use the same name for the argument and its corresponding parameter. This makes the program much easier to read and understand. Call CalculateCommission(decSalesAmount, decCommission) Private Sub CalculateCommission(ByVal decAmount As Decimal, ByRef decSalesCommission As Decimal) 27
28
6.3 Functions A Function Returns a Value to the Part of the Program That Called the Function Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
29
Explain the general form of a function procedure
Private Function FunctionName (Place one or more parameters here.) As Datatype 'Put code here. End Function Calling a Function: FunctionName( Place one or more arguments here.) Datatype for the FunctionName You use ByVal and/or ByRef here. Typically, you pass arguments to parameters By Value when using a function procedure. If you need to return a value, it is done in the FunctionName...not usually through a ByRef parameter. The programmer makes up these names: 1. FunctionName 2. Arguments and parameters names Note: The Function Name returns the result of the function code. Also note that the Function Name must be given a data type. 29
30
Function Example with No Return
How it works: 1. The function CubeNumber( decNumber) is called within the Boss module and decNumber with a value of 3 is passed to decNumber in the Function. 2. Within the Function, 9 is assigned to CubeNumber which is the Name of the Function. 3. In the Boss module CubeNumber, the name of the function, assigns 9 to CubedNumber. 4. Set a break point and follow the execution of the code. Argument Parameter 3 is passed 30
31
Function Example with With Return
How it works: 1. The function CubeNumber( decNumber) is called within the Boss module and decNumber with a value of 3 is passed to decNumber in the Function. 2. Within the Function, 9 is assigned to Return and Return then assigns 9 to the name of the Function, CubeNumber. 3. In the Boss module CubeNumber, the name of the function, assigns 9 to CubedNumber. 4. Set a break point and follow the execution of the code. Argument 3 is passed Parameter 31
32
Program Calculate Sales Commissions
Explain that function procedures will be used in the construction of classes. Program Calculate Sales Commissions This program demonstrates: 1. Hierarchy Chart 2. Function Procedures 3. Sub-Procedures 4. Passing by Value and by Reference 5. Control of “Coupling”. 6. Functional level “Cohesion” Very Important. 32
33
See if students appreciate the two points below.
VERY IMPORTANT: EXAMINE THE FOLLOWING PROGRAM CLOSELY. SEE HOW THIS PROGRAM HAS BEEN MADE EASIER TO UNDERSTAND BECAUSE OF: (1) The Hierarchy Chart, and (2) the breaking up of the program code into general procedures and function procedures that do a SINGLE FUNCTION. 33
34
Show the function procedure
Hierarchy Chart for Sales Commission Calculator Program Function Procedure Note: To the best of my knowledge there is no standard way to show an EVENT procedure on a Hierarchy Chart. The two red rectangles above are EVENT procedures. I made them red to show that they are EVENT procedures. The dotted line shows that the two red rectangles are part of the same program. (The red color and the dotted line are my own creations.) The white rectangles are general procedures and a function procedure. These are depicted in the standard way for a hierarchy chart. 34
35
Explain how this program works and run it.
GUI for Sales Commission Calculator Enter Sales Amount Display Commission 35
36
The coded procedure is below:
Explain Code The coded procedure is below: 36
37
The coded procedure is below:
Explain Code The coded procedure is below: 37
38
The coded FUNCTION is below:
Explain Code The coded FUNCTION is below: 38
39
Explain Code 39
40
Function Procedures Using The "Return" Statement
40
41
Function Procedures How the Return works:
1. First, the function is called: decSalesCommission = CalculateSalesCommission(decSalesAmount) 2. decSalesAmount is passed into the function ByVal. 3. The Return causes the result of the expression at the Return keyword’s immediate right to be placed into the name of the function. 4. For example, if decSalesAmount is then the Return Keyword would place into CalculateSalesCommission. 5. Lastly, CalculateSalesCommission value (25,000) is assigned to decSalesCommission 41
42
SUMMARY The arguments and their parameters must match in three ways:
1. Data type -Example-1: If an argument is an integer, its parameter must be an integer. -Example-2: If an argument is a decimal, its parameter must be a decimal. -Example-3:If an argument is a string, its parameter must be a string. 2. Position -Example: If argument-2 is second in the list of arguments, then its corresponding parameter-2 must be second in the parameter list. 3. Number -Example: If there are 3 arguments, then there must be 3 parameters. Arguments and their corresponding parameters need not match on: 1. Names -Example: Note the different names below. However, for this class, Dr. Scanlan wants you to ALWAYS use the same name for the argument and its corresponding parameter. This makes the program much easier to read and understand. Call CalculateCommission(decSalesAmount, decCommission) Private Sub CalculateCommission(ByVal decAmount As Decimal, ByRef decSalesCommission As Decimal) 42
43
6.4 More About Debugging Step Into Step Over Step Out
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
44
Debugging Involving Procedures
Step Into - continue to debug by single-stepping through a procedure (F11) Step Over - run procedure without single-stepping, continue single-step after the call (Shift+F11) Step Out - end single-stepping in procedure, continue single-step after the call (Ctrl+Shift+F11) Tutorial 6-6 provides examples Be sure to do this Tutorial
45
Building the Bagel and Coffee Price Calculator Application
6.5 Building the Bagel and Coffee Price Calculator Application Use procedures and functions to calculate the total of a customer order. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
46
Bagel and Coffee Price Calculator
47
Button Click Event Flowcharts
Calculate Button Reset Button
48
Cost Calculation Functions
Topping Cost Function Bagel Cost Function
49
Cost Calculations Functions
Coffee Cost Function Calc Tax Function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.