1 / 16

Lab 7 (1) Passing Arguments & Function Subroutines

Lab 7 (1) Passing Arguments & Function Subroutines. ► Lab 6 Review ► Variable Scopes ► Passing Arguments ► Function Subroutines ► Exercises. Lab 6 Exercise. Specify the data Range (You may use end property for ease) Store all customer names in an array

aradia
Download Presentation

Lab 7 (1) Passing Arguments & Function Subroutines

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab 7 (1) Passing Arguments & Function Subroutines ► Lab 6 Review ► Variable Scopes ► Passing Arguments ► Function Subroutines ► Exercises

  2. Lab 6 Exercise • Specify the data Range (You may use end property for ease) • Store all customer names in an array • Write a loop to scan every customer name • Use if construction to compare target customer name with current array element • When you get that customer name, give out information, and exit loop • If you still can’t get the customer name at the end of loop, give out information

  3. Array Review • DimArrayName (Number of Elements) AsDatatype • Employee (100) • There are 101 elements in this array • They are employee(0) to employee(100) • Use Option Base 1 or Employee (1to 100) to get rid of the mess • Dynamic Array Dim Employee () as String Redim Employee (100) Redim Preserve Employee (50) • When you shorten dynamic array data stored in shortened part will be lost

  4. Modularization of Programs (1) – Chpt 10 • So far, we have only discussed single, self-contained subs. However, when dealing with complicated tasks (e.g., the decision making support tasks of your intended DSSs), if you write all your codes in one sub, this sub can become very long. Long subroutines are hard to read, hard to debug, and hard to reuse. • As programs become longer and more complex, it is common to break them down into smaller subs, with each sub performing a specific task (Modularizing). The smaller subroutines will thus be reusable in other programs. • Through modularizing, a typical application can have many subs in one or more modules. • Different subs in an application can be related in the following ways: • They can share the same variables • Subs can call one another, and they can pass arguments (share information) when they make the call. • Example:

  5. Modularization of Programs (2) Sub Main( ) … … Call Task1 Call Task2 … … End Sub Sub Task1( ) … … Call Task3 … … End Sub Sub Task2( ) … … End Sub Sub Task3( ) … … End Sub (1) (4) (5) (6) (2) (3)

  6. Scope of Variables (1) • A variable’s scope determines whether it can be shared among different subs. • A variable may have the following 3 different scopes: • Procedure-level (local-level) Declare the variable within the procedure, e.g., Sub Example1( ) Dim Name As String, Grade As Integer Grade = 95 End SubSub Example2( ) Dim Grade As Integer Msgbox “The grade is ” & GradeEnd Sub The sub that contains the variable declaration (Example1 here) is the only sub that recognizes the variable (Name and Grade variables here). Local variables in different subs can have the same names, but they lead independent existences. • Module-level Declare variables at the top of a module before any subs with the keywordDimor keywordPrivate, e.g.,

  7. Scope of Variables (2) Dim Msg As String, N As Integer (or Private Msg As String, N As Integer) Sub Example1( ) … …End SubSub Example2 ( ) … …End Sub Every sub in the module (Example1 sub and Example2 sub) has access to the module-level variables (Msg and N variables here). • Project-level Declare variables at the top of a module with the keywordPublic, e.g., Public Msg As String, N As Integer All modules in the entire project have access to the declared variables (Msg and N variables here). This is useful when you have two or more modules in your project. It is also useful when you have “event code” for user forms – the Public variables are recognized by the event code.

  8. Scope of Subroutines • Like variables, subroutines also have scope. • As stated previously, different subs in a program can be related by calling each other. Scope of subroutines then determines which subs can call which others. • By default, all subs have public scope. To specify the scope of a sub as public you can either use the keyword Public, as in Public Sub Example1() … … End Sub or you do not have to use the keyword, as in Sub Example1() … … End Sub With a public scope, a sub can be called by any other sub in the entire project • You can also have a sub with a module-level scope using the keyword Private, as in Private Sub Example2() … … End Sub Then, only the subs in the same module as Example2 can call Example2. • Such scope of subs are the same for Function subroutines.

  9. Passing Arguments (1) • There are two ways to share variables among different subs: • Using module-level or project-level variables • Passing arguments • Example 1: Suppose a Main sub calls an Add sub to calculate the sum of two integer numbers and display the results in a message box. The following lists the two solutions – one uses module-level variables and the other uses the passing arguments method. • Using module level variables Dim X1 as Integer, X2 as IntegerSub Main( ) X1 = 2 X2 = 3 Call AddEnd SubSub Add( ) Dim Sum As Integer Sum = X1 + X2 Msgbox “The result is ” & Sum End Sub

  10. Passing Arguments (2) The calling sub and the called sub don’t need to have the same variable names, but must match in number, type, and order • Passing arguments from the Main sub to the Add sub Sub Main() Calling Sub Dim X1 As Integer, X2 As Integer X1 = 2 X2 = 3 Call Add(X1, X2) End Sub Sub Add(a As Integer, b As Integer) Dim sum As Integer sum = a + b MsgBox "The result is " & sum End Sub Called Sub Through passing arguments, X1 and X2 do not need to be declared as module-level variables. They are declared locally in Main sub and then passed to Add sub as arguments

  11. Passing Arguments Summary • To call a sub, write the name of the called sub and then list the variables being passed, separated by “,” and included within (). • In the called sub, the arguments should be declared (Dim is not used) inside parentheses next to the name of the sub, e.g., Sub Add(a As Integer, b As Integer) • The passed arguments do not need to have the same names in the calling and called subs, but they must match regarding number, type, and order. • Compared with using module-level variables, the passing arguments method can do the same thing in a more structured way. Self-contained subs can be reused, exactly as it stands, in a different program, independent of a list of module-level variables.

  12. Function Subroutines • A special type of subroutine: return a certain type of variable • Syntax:FunctionFunctionName ([Argumentlist]) [As Type] … … [FunctionName = expression] (The type of the returned value)End Function • When a function finishes, it returns the value assigned to its name. • A function can be called by another sub (or even another function subroutine), or it can be used as an Excel function (e.g., SUMPRODUCT, SUM)

  13. Examples of Function Subroutines (1) • The previous passing arguments example can be rewritten as the following: Sub Main() Dim X1 As Integer, X2 As Integer X1 = 2 X2 = 3 MsgBox "The result is " & Add(X1, X2) End Sub Function Add(a As Integer, b As Integer) As Integer Add = a + b End Function

  14. Examples of Function Subroutines (2) • Example2: Return the larger number between 2 integers Sub Comparison() Dim a As Integer, b As Integer a = 3: b = 4 MsgBox "The larger one is " & Larger(a, b) End Sub Function Larger(num1 As Integer, num2 As Integer) As Integer 'Return the larger number If num1 >= num2 Then Larger = num1 Else Larger = num2 End If End Function • To write multiple instructions on a single line, use colon “:”. For example, Sub OneLine( ) x = 1: y = 2: z = 3: MsgBox x + y + zEnd Sub

  15. Examples of Function Subroutines (3) • Example3: passing array. Reconsider the previous array example of calculating the average as having two subs (Main sub and MyAverage function). In the Main sub, 1) ask the user to input 10 numbers; 2) store these numbers in an array named Num; 3) call the MyAverage function by passing the array; and 4) Display the result (the average of the numbers) to the user. Option Base 1 Sub Main() Dim i As Integer, Num() As Integer, N As Integer N = 10 ReDim Num(N) For i = 1 To N Num(i) = InputBox("Please enter number " & i & ":") Next i MsgBox "The average of these numbers is " & MyAverage(Num) End Sub Function MyAverage(Number() As Integer) As Single Dim i As Integer, Total As Integer Total = 0 For i = 1 To UBound(Number) Total = Total + Number(i) Next i MyAverage = Total / UBound(Number) End Function No () is next to MyAverage in this line. Here: () are needed to indicate MyAverage Function is expecting an array. UBound returns the largest index in the array

  16. Assignment 1 Download Lab 6 Student Array data from the Blackboard site and rewrite the Customer List program to have two subs: • The first Sub pops up an InputBox to ask the user for a customer name and calls the second Sub. • The second Sub uses the input to check whether the name is on the customer list. If it is, display an msgbox saying that customer name is on the list, bold and activate the cell. Otherwise, display an msgbox saying that customer name is not on the list. • Hints again: Dim the customer list as an array and Dim Found as Boolean. You will need If-Then construct and For-Next or Do-While/Until loop too. (Make sure that you declare all of your variables appropriately.)

More Related