Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)"— Presentation transcript:

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

2 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 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 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 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 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 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 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 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 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 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 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 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: 2 2 6 2

14 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 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 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 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 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 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 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 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 22 Sub Add Private Sub Add(num As Integer) Dim var1 As Integer, var2 As Integer num = var1 + var2 End Sub

23 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 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 25 Example ' In Declaration section of General Dim num1 As Single, num2 As Single


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

Similar presentations


Ads by Google