sub procedures school of business eastern illinois university © abdou illia, spring 2002 (week 6,...

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

Upload: marcus-parker

Post on 02-Jan-2016

217 views

Category:

Documents


3 download

TRANSCRIPT

Sub procedures

School of BusinessEastern 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

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

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

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

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

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

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.

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)

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

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)

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: _________________

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

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

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

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

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

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.

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

20Advantages of Local Variables

Extremely useful for team programming

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

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

22Sub Add

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

23Form-Level Variables

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

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

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

25Example

' In Declaration section of General

Dim num1 As Single, num2 As Single