Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VB Numbers and Strings School of Business Eastern Illinois University (Week 4, Monday 2/03/2003) © Abdou Illia, Spring 2003.
1.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Passing Arguments Question Example: IS1102 Exam Autumn 2001.
VBA Modules, Functions, Variables, and Constants
Example 2.
VB Built-in Functions School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 5, Monday 2/10/03)
Chapter 4 - Visual Basic Schneider
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
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.
Two-Dimensional Arrays School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 11, Friday 4/04/2003)
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Programming with Microsoft Visual Basic 2012 Chapter 7: Sub and Function Procedures.
Variables and Constants
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Tutorial 11 Using and Writing Visual Basic for Applications Code
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
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)
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
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
® Microsoft Access 2010 Tutorial 11 Using and Writing Visual Basic for Applications Code.
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.
Array Processing: Exercises School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 10, Friday 3/28/2003)
CS0004: Introduction to Programming Subprocedures and Modular Design.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Fundamentals of GUI Programming. Objectives: At the end of the session, you should be able to: describe the guidelines that are used for creating user-friendly.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Sub Procedures. A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, general procedures.
Sequential files School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 12, Monday 4/07/2003)
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 15, Friday 5/2/2003)
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
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.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
COMPREHENSIVE Access Tutorial 11 Using and Writing Visual Basic for Applications Code.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Select Case Blocks School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 8, Monday 3/03/2003)
Programming Right from the Start with Visual Basic .NET 1/e
A variable is a name for a value stored in memory.
Using Procedures and Exception Handling
Objectives Learn about Function procedures (functions), Sub procedures (subroutines), and modules Review and modify an existing subroutine in an event.
Variables and Arithmetic Operations
Exploring Microsoft Office Access 2007
Chapter 4 - Visual Basic Schneider
Microsoft Visual Basic 2005 BASICS
6 Chapter Functions.
Topics Introduction to Functions Defining and Calling a Function
Chapter 8 - Functions and Functionality
Tutorial 11 Using and Writing Visual Basic for Applications Code
Presentation transcript:

Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)

2 Learning Objectives n Creating Visual Basic Sub Procedures n Creating User-defined Function Procedures n Parameter Passing Mechanism n Modularizing in Programming Languages

3 What is Modularization n So far, in our programs many tasks performed by a single event procedure like this one: Private Sub cmdCalculateDisplay_Click() MonthlyDeposit = Val(txtDeposit.Text) YearlyInterest = Val(txtYearlyInterest.Text) NumberOfMonths = Val(txtMonths.Text) MonthlyRate = YearlyInterest / 1200 FinalBalance = MonthlyDeposit * ((1 + MonthlyRate) ^ NumberOfMonths - 1) / MonthlyRate picOutput.Print txtNumber.Text; Tab(15); MonthlyDeposit; Tab(30); YearlyInterest; Tab(45); NumberOfMonths; Tab(55); FormatCurrency(FinalBalance, 2) txtNumber.Text = "" txtName.Text = "" txtDeposit.Text = "" txtYearlyInterest.Text = "" txtMonths.Text = "" txtNumber.SetFocus End Sub n This is too many tasks performed by a single procedure – Not easy to read. Not easy to write. Need to be broken into subtasks (or modules).

4 What is Modularization n A programming technique n Breaking a program into modules – That perform specific subtasks Private Sub cmdSummarize_Click() Statement 1 Statement 2 Call Module1 Call Module2 End Sub Private Sub SubProcedure1() Statement 1 : Statement n End Sub Private Sub SubProcedure2() Statement 1 : Statement n End Sub Main Module Module 1 Module 2

5 Modularizing Programs in Visual Basic n In Visual Basic, there are three types of procedures: – Event procedures – Sub procedures – Functions Note: To distinguish them from event procedures, Sub procedures and Functions are referred to as general procedures. n Call statements are used to call Sub procedures and Functions Private Sub cmdSummarize_Click() Statement 1 Statement 2 Call SubProcedureName Call FunctionName End Sub Main Module

6 Sub Procedures Properties n may be called n may be passed data called arguments n may return values to the calling program Call SubProcedureName (x, y) Arguments Call SubProcedureName (x+2, 4*y) Arguments Call CalculateFinalBalance(MonthlyDeposit, YearlyInterest, NumberOfMonths) Example

7 Sub Procedures Properties n SubprocedureName: Identify the Sub procedure n parameters: a Sub procedure accepts values from the caller through its parameters; it may also send values back to the caller through it’s parameters. [Public] [Private] Sub SubProcedureName (a As type, b As Type) Statements End Sub Parameters Private Sub CalculateFinalBalance(MonthlyDeposit As Single, YearlyInterest As Single, NumberOfMonths As Integer) MonthlyRate = YearlyInterest / 1200 FinalBalance = MonthlyDeposit * ((1 + MonthlyRate) ^ NumberOfMonths - 1) / MonthlyRate End Sub Syntax Example

8 Sub Procedure's Name n The rules for naming Sub Procedures are the same as naming variables. – Must begin with a letter. – Can contain letters, numeric digits. – Can have up to 255 characters. – Can Not be restricted keyword.

9 Passing Arguments to Sub Procedures n Arguments : Data items placed in parentheses in a Call statement. n Arguments can be constants, variables or expressions Call Add (2, 6) Call Add (num1, num2) Call Add (num1, 3*num2)

10 Parameters n Variables placed in parentheses after a Sub Procedure's name. n When the procedure is called, the values of the corresponding arguments are placed in the parameters. n Call Add (x, y ) n Private Sub Add ( num1 As Single, num2 As Single) Parameters Arguments

11 Important Rules for Passing Arguments to a Sub n The number of arguments and parameters must match. n The data type of each argument must match its corresponding parameter. n The order is important Call Add (x, y ) Private Sub Add ( num1 As Single, num2 As Single)

12 Passing Arguments By Reference n The argument is passed as a variable (or as a reference). – After execution of the Sub procedure, the argument may have a different value than before. Private Sub cmdDisplay_Click() Dim amt As Single picResults.Cls amt = 2 picResults.Print amt; Call Triple(amt) picResults.Print amt End Sub Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub Result after execution: _________________

13 Passing Arguments By Value n The value of the argument is passed (not a reference). – After execution of the Sub procedure, value of the argument remain the same. n Syntax: Call Add ((amt)) or Private Sub Triple(ByVal num As Single) Private Sub cmdDisplay_Click() Dim amt As Single picResults.Cls amt = 2 picResults.Print amt; Call Triple((amt)) picResults.Print amt End Sub Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num; End Sub Result after execution:

14 Creating Visual Basic Sub Procedure: n Activate a code window n Select Add Procedure from the Tools menu n Type in the name of the Sub procedure n Click Sub in Type frame n Click Private or Public in Scope frame n Press the Enter key or click the OK button n Add parameters names and types in parentheses n Type the statements of the Sub procedure Note: We can create Sub procedures by typing directly in the code Window

15 Exercise: Account Balance (Project 2) n Main tasks performed: – Assign values variables – Compute Final balance – Display input data an Final balance in picOutput – Delete content of text boxes. Private Sub cmdCalculateDisplay_Click() Dim MonthlyDeposit As Single, YearlyInterest As Single Dim NumberOfMonths As Integer MonthlyDeposit = Val(txtDeposit.Text) YearlyInterest = Val(txtYearlyInterest.Text) NumberOfMonths = Val(txtMonths.Text) MonthlyRate = YearlyInterest / 1200 FinalBalance = MonthlyDeposit * ((1 + MonthlyRate) ^ NumberOfMonths - 1) / MonthlyRate picOutput.Print txtNumber.Text; Tab(15); MonthlyDeposit; Tab(30); YearlyInterest; Tab(45); _ NumberOfMonths; Tab(55); FormatCurrency(FinalBalance, 2) txtNumber.Text = "" txtName.Text = "" txtDeposit.Text = "" txtYearlyInterest.Text = "" txtMonths.Text = "" txtNumber.SetFocus End Sub SpaceBar _ ENTER to continue on another line

16 Exercise: Account Balance (Project 2) Private Sub cmdCalculateDisplay_Click() Dim MonthlyDeposit As Single, YearlyInterest As Single Dim NumberOfMonths As Integer MonthlyDeposit = Val(txtDeposit.Text) YearlyInterest = Val(txtYearlyInterest.Text) NumberOfMonths = Val(txtMonths.Text) Call CalculateDisplayBalance(MonthlyDeposit, YearlyInterest, NumberOfMonths) txtNumber.Text = "" txtName.Text = "" txtDeposit.Text = "" txtYearlyInterest.Text = "" txtMonths.Text = "" txtNumber.SetFocus End Sub Private Sub CalculateDisplayBalance (MonthlyDeposit As Single, YearlyInterest As Single, NumberOfMonths As Integer) MonthlyRate = YearlyInterest / 1200 FinalBalance = MonthlyDeposit * ((1 + MonthlyRate) ^ NumberOfMonths - 1) / MonthlyRate picOutput.Print txtNumber.Text; Tab(15); MonthlyDeposit; Tab(30); YearlyInterest; Tab(45); _ NumberOfMonths; Tab(55); FormatCurrency(FinalBalance, 2) End Sub SpaceBar _ ENTER to continue on another line

17 Exercise: Account Balance (Project 2) Private Sub cmdCalculateDisplay_Click() Dim MonthlyDeposit As Single, YearlyInterest As Single Dim NumberOfMonths As Integer MonthlyDeposit = Val(txtDeposit.Text) YearlyInterest = Val(txtYearlyInterest.Text) NumberOfMonths = Val(txtMonths.Text) Call CalculateDisplayBalance(MonthlyDeposit, YearlyInterest, NumberOfMonths) Call DeleteTextBoxes End Sub Private Sub DeleteTextBoxes() txtNumber.Text = "" txtName.Text = "" txtDeposit.Text = "" txtYearlyInterest.Text = "" txtMonths.Text = "" txtNumber.SetFocus End Sub

18 Local Variables: n A variable that is used only in a specific procedure (Sub or Function). n The scope of the local variable is the portion of a Sub or Function in which that variable has been defined.

19 Local Variables: n Declared within a procedure definition n Private to a procedure definition n Variables in different procedures are totally independent n different procedures can have variables with the same names; however, each variable will have its own memory location

20 Advantages of Local Variables n Extremely useful for team programming n To protect side effect (which is an accidental change of the value of the variable)

21 Example of Local Variables Private Sub cmdButton_Click() Dim var1 As Integer, var2 As Integer,num As Integer var1 = 2 var2 = 4 Call Add(num) picBox.Print num End Sub Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub

22 Sub Add Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub

23 Form-Level Variables n Form-level variables are visible to every procedure (Global variable). n Form-level variables appear at the top of the code window.

24 How to create Form-Level Variables? n Activate the code window n Click on the down arrow to the right of the object list box n Click on General n Click on Declaration in the procedure list box n Type in Dim statements for form-level variables

25 Example ' In Declaration section of General Dim num1 As Single, num2 As Single