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

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

Jan 02, 2016

Download

Documents

Marcus Parker
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)

Sub procedures

School of BusinessEastern Illinois University

© Abdou Illia, Spring 2002

(Week 6, Friday 2/21/03)

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

2Learning Objectives

Creating Visual Basic Sub Procedures

Creating User-defined Function Procedures

Parameter Passing Mechanism

Modularizing in Programming Languages

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

3What is Modularization

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.SetFocusEnd Sub

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).

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

4What is Modularization

A programming technique

Breaking a program into modules– That perform specific subtasks

Private Sub cmdSummarize_Click() Statement 1 Statement 2 Call Module1 Call Module2End Sub

Private Sub SubProcedure1() Statement 1 : Statement nEnd Sub

Private Sub SubProcedure2() Statement 1 : Statement nEnd Sub

Main Module

Module 1

Module 2

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

5Modularizing Programs in Visual Basic

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.

Call statements are used to call Sub procedures and Functions

Private Sub cmdSummarize_Click() Statement 1 Statement 2 Call SubProcedureName Call FunctionNameEnd Sub

Main Module

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

6Sub Procedures Properties

may be called

may be passed data called arguments

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

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

7Sub Procedures Properties

SubprocedureName: Identify the Sub procedure

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 SubParameters

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

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

8Sub Procedure's Name

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.

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

9Passing Arguments to Sub Procedures

Arguments : Data items placed in parentheses in a Call statement.

Arguments can be constants, variables or expressions

Call Add (2, 6)

Call Add (num1, num2)

Call Add (num1, 3*num2)

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

10Parameters

Variables placed in parentheses after a Sub Procedure's name.

When the procedure is called, the values of the corresponding arguments are placed in the parameters.

Call Add (x, y )

Private Sub Add ( num1 As Single, num2 As Single)

Parameters

Arguments

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

11Important Rules for Passing Arguments to a Sub

The number of arguments and parameters must match.

The data type of each argument must match its corresponding parameter.

The order is important

Call Add (x, y )

Private Sub Add ( num1 As Single, num2 As Single)

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

12Passing Arguments By Reference

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 amtEnd Sub

Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num;End Sub

Result after execution: _________________

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

13Passing Arguments By Value

The value of the argument is passed (not a reference).– After execution of the Sub procedure, value of the

argument remain the same.

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 amtEnd Sub

Private Sub Triple(num As Single) 'Triple a number picResults.Print num; num = 3 * num picResults.Print num;End Sub

Result after execution: 2 2 6 2

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

14Creating Visual Basic Sub Procedure:

Activate a code window

Select Add Procedure from the Tools menu

Type in the name of the Sub procedure

Click Sub in Type frame

Click Private or Public in Scope frame

Press the Enter key or click the OK button

Add parameters names and types in parentheses

Type the statements of the Sub procedure

Note: We can create Sub procedures by typing directly in the code Window

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

15Exercise: Account Balance (Project 2)

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.SetFocusEnd Sub

SpaceBar _ ENTER to continue on another line

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

16Exercise: 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.SetFocusEnd 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 SubSpaceBar _ ENTER to continue on another line

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

17Exercise: 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 DeleteTextBoxesEnd Sub

Private Sub DeleteTextBoxes()

txtNumber.Text = ""

txtName.Text = ""

txtDeposit.Text = ""

txtYearlyInterest.Text = ""

txtMonths.Text = ""

txtNumber.SetFocus

End Sub

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

18Local Variables:

A variable that is used only in a specific procedure (Sub or Function).

The scope of the local variable is the portion of a Sub or Function in which that variable has been defined.

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

19Local Variables:

Declared within a procedure definition

Private to a procedure definition

Variables in different procedures are totally independent

different procedures can have variables with the same names; however, each variable will have its own memory location

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

20Advantages of Local Variables

Extremely useful for team programming

To protect side effect (which is an accidental change of the value of the variable)

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

21Example 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

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

22Sub Add

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

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

23Form-Level Variables

Form-level variables are visible to every procedure (Global variable).

Form-level variables appear at the top of the code window.

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

24How to create Form-Level Variables?

Activate the code window

Click on the down arrow to the right of the object list box

Click on General

Click on Declaration in the procedure list box

Type in Dim statements for form-level variables

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

25Example

' In Declaration section of General

Dim num1 As Single, num2 As Single